mysql 视图

 创建视图

mysql> select * from test;
+----+------------+-------+-----------+
| id | name       | score | subject   |
+----+------------+-------+-----------+
|  1 | xiaoming   |    89 | shuxue    |
|  2 | xiaohong   |    89 | shuxue    |
|  3 | xiaohong   |    80 | english   |
|  4 | xiaohong   |    80 | physics   |
|  5 | xiaohong   |    80 | astronaut |
|  6 | xiaoming   |    80 | physics   |
|  7 | xiaoming   |    80 | astronaut |
|  8 | xiaoming   |    80 | english   |
|  9 | xiaobai    |    80 | astronaut |
| 10 | 1.2xiaobai |    80 | english   |
| 11 | 2.2xiaobai |    80 | physics   |
| 12 | 3xiaobai   |    80 | shuxue    |
| 13 | 123xiaohei |    80 | astronaut |
| 14 | xiaohei    |    80 | shuxue    |
| 15 | xiaohei    |    80 | physics   |
| 16 | .12xiaohei |    80 | english   |
+----+------------+-------+-----------+
16 rows in set (0.00 sec)



mysql> select * from user;
+------+----------+-----------+
| id   | name     | address   |
+------+----------+-----------+
|    2 | xiaobai  | shandong  |
|    3 | xiaohong | suzhou    |
|    4 | xiaohei  | changchun |
|    1 | xiaoming | beijing   |
+------+----------+-----------+
4 rows in set (0.00 sec)



mysql> select test.name,score,subject,address from test,user where user.name=test.name;
+----------+-------+-----------+-----------+
| name     | score | subject   | address   |
+----------+-------+-----------+-----------+
| xiaoming |    89 | shuxue    | beijing   |
| xiaohong |    89 | shuxue    | suzhou    |
| xiaohong |    80 | english   | suzhou    |
| xiaohong |    80 | physics   | suzhou    |
| xiaohong |    80 | astronaut | suzhou    |
| xiaoming |    80 | physics   | beijing   |
| xiaoming |    80 | astronaut | beijing   |
| xiaoming |    80 | english   | beijing   |
| xiaobai  |    80 | astronaut | shandong  |
| xiaohei  |    80 | shuxue    | changchun |
| xiaohei  |    80 | physics   | changchun |
+----------+-------+-----------+-----------+
11 rows in set (0.00 sec)



mysql> create view testview as (select test.name,score,subject,address from test,user where user.name=test.name);
Query OK, 0 rows affected (0.01 sec)



mysql> select * from testview;
+----------+-------+-----------+-----------+
| name     | score | subject   | address   |
+----------+-------+-----------+-----------+
| xiaoming |    89 | shuxue    | beijing   |
| xiaohong |    89 | shuxue    | suzhou    |
| xiaohong |    80 | english   | suzhou    |
| xiaohong |    80 | physics   | suzhou    |
| xiaohong |    80 | astronaut | suzhou    |
| xiaoming |    80 | physics   | beijing   |
| xiaoming |    80 | astronaut | beijing   |
| xiaoming |    80 | english   | beijing   |
| xiaobai  |    80 | astronaut | shandong  |
| xiaohei  |    80 | shuxue    | changchun |
| xiaohei  |    80 | physics   | changchun |
+----------+-------+-----------+-----------+
11 rows in set (0.00 sec)

 查看视图

mysql> show create view testview;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View     | Create View                                                                                                                                                                                                                                                                  | character_set_client | collation_connection |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| testview | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `testview` AS (select `test`.`name` AS `name`,`test`.`score` AS `score`,`test`.`subject` AS `subject`,`user`.`address` AS `address` from (`test` join `user`) where (`user`.`name` = `test`.`name`)) | utf8                 | utf8_general_ci      |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

删除视图

mysql> drop view testview;
Query OK, 0 rows affected (0.00 sec)

 

 

 参考:

https://www.2cto.com/database/201803/726747.html

 

posted @ 2019-08-07 13:40  anobscureretreat  阅读(164)  评论(0编辑  收藏  举报