合并结果集(UNION)
GaussDB 100提供UNION运算符,将多个查询块的结果集合并为一个结果集输出。
语法格式
select_statement UNION [ALL] select_subquery
使用方法
每个查询块的查询列数目必须相同。
每个查询块对应的查询列必须为相同数据类型或同一数据类型组,不同数据类型组或不支持组合的数据类型不允许执行UNION操作。
差异结果集(MINUS)
GaussDB 100提供MINUS运算符,对查询结果集的减法。
A minus B C就意味着将结果集A去除结果集B和结果集C中所包含的所有记录后的结果,即在A中存在,而在B、C中不存在的记录。
语法格式
select_statement MINUS select_statement2 [ ... ]
参数说明
select_statement1
产生第一个结果集的SELECT语句。
select_statement2
产生第二个结果集的SELECT语句。
示例
使用MINUS查询数据。
--删除表education。
DROP TABLE IF EXISTS education;
--创建表education。
CREATE TABLE education(staff_id INT, highest_degree CHAR(8) NOT NULL, graduate_school VARCHAR(64), graduate_date DATETIME, education_note VARCHAR(70));
--向表education中INSERT记录1。
INSERT INTO education(staff_id,highest_degree,graduate_school,graduate_date,education_note) VALUES(10,'博士','西安电子科技大学','2017-07-06 12:00:00','211');
--向表education中INSERT记录2。
INSERT INTO education(staff_id,highest_degree,graduate_school,graduate_date,education_note) VALUES(11,'硕士','西北工业大学','2017-07-06 12:00:00','211和985');
--向表education中INSERT记录3。
INSERT INTO education(staff_id,highest_degree,graduate_school,graduate_date,education_note) VALUES(12,'学士','西安建筑科技学院','2017-07-06 12:00:00','非211和985');
--提交事务。
COMMIT;
--删除表education_disable。
DROP TABLE IF EXISTS education_disable;
--创建表education_disable。
CREATE TABLE education_disable(staff_id INT, highest_degree CHAR(8) NOT NULL, graduate_school VARCHAR(64), graduate_date DATETIME, education_note VARCHAR(70));
--向表education_disable中INSERT记录。
INSERT INTO education_disable(staff_id,highest_degree,graduate_school,graduate_date,education_note) VALUES(10,'doctor','Xidian University','2017-07-06 12:00:00','211');
--提交事务。
COMMIT;
--使用MINUS查询数据。
SELECT * FROM education MINUS SELECT * FROM education_disable WHERE staff_id=10;