一、SQL语法——13-集合运算
集合运算
1.select语句的查询救过是一个包含多条数据的结果集,类似于数学中的集合,可以进行intersect、union、minus运算,为了对两个结果集进行集合运算,这两个结果集必须满足如下条件:
(1)两个结果集所包含的数据列的数量相同;
(2)两个结果集包含的数据列的数据也必须一一对应。
2.示例:
/* union运算的语法格式: select 语句 union select 语句 */ --查询出教师信息和主键小于4的学生信息 select * from teacher_table union select student_id,student_name from student_table; /* minus运算的语法格式: select 语句 minus select 语句 */ --从所有学生中减去与老师记录的ID相同、姓名相同的记录 select student_id,student_name from student_table minus select teacher_id,teacher_name from teacher_table; /* intersect运算的语法格式: */ --查询出学生记录中与老师记录中ID相同,名字相同的记录 select student_id,student_name from student_table intersect select teacher_id,teacher_name from teacher_table;