首页 > 数据库 > > Gauss 100 数据查询之WITH AS子句
定义一个SQL片断,该SQL片断会被整个SQL语句用到。
可以使SQL语句的可读性更高。存储SQL片段的表与基本表不同,是一个虚表。数据库不存放视图对应的定义和数据,这些数据仍存放在原来的基本表中。若基本表中的数据发生变化,从存储SQL片段的表中查询出的数据也随之改变。
WITH { table_name AS select_statement1 }[ , ...] select_statement2
table_name
用户自定义的存储SQL片段的表的名称。
select_statement1
从基本表中查询数据的SELECT语句。
select_statement2
从用户自定义的存储SQL片段的表中查询数据的SELECT语句。
使用WITH AS查询数据。
--删除表education。
DROP TABLE IF EXISTS education;
--创建表education。
CREATE TABLE education(staff_id INT, higest_degree CHAR(8) NOT NULL, graduate_school VARCHAR(64),graduate_date DATETIME, education_note VARCHAR(70));
--向表education中加入记录1。
INSERT INTO education(staff_id,higest_degree,graduate_school,graduate_date,education_note) VALUES(10,'doctor','Xidian University','2017-07-06 12:00:00','211');
--向表education中加入记录2。
INSERT INTO education(staff_id,higest_degree,graduate_school,graduate_date,education_note) VALUES(11,'master','Northwestern Polytechnical University','2017-07-06 12:00:00','211&985');
--向表education中加入记录3。
INSERT INTO education(staff_id,higest_degree,graduate_school,graduate_date,education_note) VALUES(12,'scholar','Xi’an University of Architecture and Technology','2017-07-06 12:00:00','not 211 or 985');
--提交事务。
COMMIT;
--使用WITH AS查询数据。
WITH tmp AS (SELECT staff_id, higest_degree FROM education) SELECT * FROM tmp;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69959250/viewspace-3130791/ ,如需转载,请注明出处,否则将追究法律责任。