CREATE VIEW

语法说明

视图是基于 SQL 语句的结果集的可视化的表。

视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。

您可以向视图添加 SQL 函数,WHERE 或者 JOIN 语句,也同样可以呈现数据,类似于这些数据来自于某个单一的表一样。

CREATE VIEW 语句用于创建一个视图。

语法结构

  1. > CREATE [ OR REPLACE ] VIEW view_name AS
  2. SELECT column1, column2, ...
  3. FROM table_name
  4. WHERE condition;

Note

视图总是显示最新的数据。每当你查询视图时,数据库引擎通过使用视图的 SQL 语句重建数据。

示例

  • 示例 1:
  1. CREATE TABLE t00(a INTEGER);
  2. INSERT INTO t00 VALUES (1),(2);
  3. CREATE TABLE t01(a INTEGER);
  4. INSERT INTO t01 VALUES (1);
  5. CREATE OR REPLACE VIEW v0 AS SELECT t00.a, t01.a AS b FROM t00 LEFT JOIN t01 USING(a);
  6. mysql> SELECT t00.a, t01.a AS b FROM t00 LEFT JOIN t01 USING(a);
  7. +------+------+
  8. | a | b |
  9. +------+------+
  10. | 1 | 1 |
  11. | 2 | NULL |
  12. +------+------+
  13. 2 rows in set (0.01 sec)
  14. mysql> SELECT * FROM v0 WHERE b >= 0;
  15. +------+------+
  16. | a | b |
  17. +------+------+
  18. | 1 | 1 |
  19. +------+------+
  20. 1 row in set (0.01 sec)
  21. mysql> SHOW CREATE VIEW v0;
  22. +------+---------------------------------------------------------------------------------------+
  23. | View | Create View |
  24. +------+---------------------------------------------------------------------------------------+
  25. | v0 | CREATE OR REPLACE VIEW v0 AS SELECT t00.a, t01.a AS b FROM t00 LEFT JOIN t01 USING(a) |
  26. +------+---------------------------------------------------------------------------------------+
  27. 1 row in set (0.00 sec)
  • 示例 2:
  1. drop table if exists t1;
  2. create table t1 (id int,ti tinyint unsigned,si smallint,bi bigint unsigned,fl float,dl double,de decimal,ch char(20),vch varchar(20),dd date,dt datetime);
  3. insert into t1 values(1,1,4,3,1113.32,111332,1113.32,'hello','subquery','2022-04-28','2022-04-28 22:40:11');
  4. insert into t1 values(2,2,5,2,2252.05,225205,2252.05,'bye','sub query','2022-04-28','2022-04-28 22:40:11');
  5. insert into t1 values(3,6,6,3,3663.21,366321,3663.21,'hi','subquery','2022-04-28','2022-04-28 22:40:11');
  6. insert into t1 values(4,7,1,5,4715.22,471522,4715.22,'good morning','my subquery','2022-04-28','2022-04-28 22:40:11');
  7. insert into t1 values(5,1,2,6,51.26,5126,51.26,'byebye',' is subquery?','2022-04-28','2022-04-28 22:40:11');
  8. insert into t1 values(6,3,2,1,632.1,6321,632.11,'good night','maybe subquery','2022-04-28','2022-04-28 22:40:11');
  9. insert into t1 values(7,4,4,3,7443.11,744311,7443.11,'yes','subquery','2022-04-28','2022-04-28 22:40:11');
  10. insert into t1 values(8,7,5,8,8758.00,875800,8758.11,'nice to meet','just subquery','2022-04-28','2022-04-28 22:40:11');
  11. insert into t1 values(9,8,4,9,9849.312,9849312,9849.312,'see you','subquery','2022-04-28','2022-04-28 22:40:11');
  12. drop table if exists t2;
  13. create table t2 (id int,ti tinyint unsigned,si smallint,bi bigint unsigned,fl float,dl double,de decimal,ch char(20),vch varchar(20),dd date,dt datetime);
  14. insert into t2 values(1,1,4,3,1113.32,111332,1113.32,'hello','subquery','2022-04-28','2022-04-28 22:40:11');
  15. insert into t2 values(2,2,5,2,2252.05,225205,2252.05,'bye','sub query','2022-04-28','2022-04-28 22:40:11');
  16. insert into t2 values(3,6,6,3,3663.21,366321,3663.21,'hi','subquery','2022-04-28','2022-04-28 22:40:11');
  17. insert into t2 values(4,7,1,5,4715.22,471522,4715.22,'good morning','my subquery','2022-04-28','2022-04-28 22:40:11');
  18. insert into t2 values(5,1,2,6,51.26,5126,51.26,'byebye',' is subquery?','2022-04-28','2022-04-28 22:40:11');
  19. insert into t2 values(6,3,2,1,632.1,6321,632.11,'good night','maybe subquery','2022-04-28','2022-04-28 22:40:11');
  20. insert into t2 values(7,4,4,3,7443.11,744311,7443.11,'yes','subquery','2022-04-28','2022-04-28 22:40:11');
  21. insert into t2 values(8,7,5,8,8758.00,875800,8758.11,'nice to meet','just subquery','2022-04-28','2022-04-28 22:40:11');
  22. insert into t2 values(9,8,4,9,9849.312,9849312,9849.312,'see you','subquery','2022-04-28','2022-04-28 22:40:11');
  23. mysql> select * from (select * from t1) sub where id > 4;
  24. +------+------+------+------+----------+---------+------+--------------+----------------+------------+---------------------+
  25. | id | ti | si | bi | fl | dl | de | ch | vch | dd | dt |
  26. +------+------+------+------+----------+---------+------+--------------+----------------+------------+---------------------+
  27. | 5 | 1 | 2 | 6 | 51.26 | 5126 | 51 | byebye | is subquery? | 2022-04-28 | 2022-04-28 22:40:11 |
  28. | 6 | 3 | 2 | 1 | 632.1 | 6321 | 632 | good night | maybe subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  29. | 7 | 4 | 4 | 3 | 7443.11 | 744311 | 7443 | yes | subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  30. | 8 | 7 | 5 | 8 | 8758 | 875800 | 8758 | nice to meet | just subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  31. | 9 | 8 | 4 | 9 | 9849.312 | 9849312 | 9849 | see you | subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  32. +------+------+------+------+----------+---------+------+--------------+----------------+------------+---------------------+
  33. 5 rows in set (0.01 sec)
  34. create view v1 as select * from (select * from t1) sub where id > 4;
  35. create view v2 as select ti as t,fl as f from (select * from t1) sub where dl <> 4;
  36. create view v3 as select * from (select ti as t,fl as f from t1 where dl <> 4) sub;
  37. create view v4 as select id,min(ti) from (select * from t1) sub group by id;
  38. create view v5 as select * from (select id,min(ti) from (select * from t1) t1 group by id) sub;
  39. mysql> select * from v1;
  40. +------+------+------+------+----------+---------+------+--------------+----------------+------------+---------------------+
  41. | id | ti | si | bi | fl | dl | de | ch | vch | dd | dt |
  42. +------+------+------+------+----------+---------+------+--------------+----------------+------------+---------------------+
  43. | 5 | 1 | 2 | 6 | 51.26 | 5126 | 51 | byebye | is subquery? | 2022-04-28 | 2022-04-28 22:40:11 |
  44. | 6 | 3 | 2 | 1 | 632.1 | 6321 | 632 | good night | maybe subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  45. | 7 | 4 | 4 | 3 | 7443.11 | 744311 | 7443 | yes | subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  46. | 8 | 7 | 5 | 8 | 8758 | 875800 | 8758 | nice to meet | just subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  47. | 9 | 8 | 4 | 9 | 9849.312 | 9849312 | 9849 | see you | subquery | 2022-04-28 | 2022-04-28 22:40:11 |
  48. +------+------+------+------+----------+---------+------+--------------+----------------+------------+---------------------+
  49. 5 rows in set (0.00 sec)
  50. mysql> select * from v2;
  51. +------+----------+
  52. | t | f |
  53. +------+----------+
  54. | 1 | 1113.32 |
  55. | 2 | 2252.05 |
  56. | 6 | 3663.21 |
  57. | 7 | 4715.22 |
  58. | 1 | 51.26 |
  59. | 3 | 632.1 |
  60. | 4 | 7443.11 |
  61. | 7 | 8758 |
  62. | 8 | 9849.312 |
  63. +------+----------+
  64. 9 rows in set (0.00 sec)
  65. mysql> select * from v3;
  66. +------+----------+
  67. | t | f |
  68. +------+----------+
  69. | 1 | 1113.32 |
  70. | 2 | 2252.05 |
  71. | 6 | 3663.21 |
  72. | 7 | 4715.22 |
  73. | 1 | 51.26 |
  74. | 3 | 632.1 |
  75. | 4 | 7443.11 |
  76. | 7 | 8758 |
  77. | 8 | 9849.312 |
  78. +------+----------+
  79. 9 rows in set (0.00 sec)
  80. mysql> select * from v4;
  81. +------+---------+
  82. | id | min(ti) |
  83. +------+---------+
  84. | 1 | 1 |
  85. | 2 | 2 |
  86. | 3 | 6 |
  87. | 4 | 7 |
  88. | 5 | 1 |
  89. | 6 | 3 |
  90. | 7 | 4 |
  91. | 8 | 7 |
  92. | 9 | 8 |
  93. +------+---------+
  94. 9 rows in set (0.00 sec)
  95. mysql> select * from v5;
  96. +------+---------+
  97. | id | min(ti) |
  98. +------+---------+
  99. | 1 | 1 |
  100. | 2 | 2 |
  101. | 3 | 6 |
  102. | 4 | 7 |
  103. | 5 | 1 |
  104. | 6 | 3 |
  105. | 7 | 4 |
  106. | 8 | 7 |
  107. | 9 | 8 |
  108. +------+---------+
  109. 9 rows in set (0.01 sec)