联合查询
多条查询结果合并成一个结果。
select * from employees where department_id >50 or email like ‘%a%’;
转换为联合查询:
select * from employees where department_id >50
union
select * from employees where email like '%a%';
应用场景:要查询的结果来自多个表,但两表没有直接联系,但查询的信息一样。
特点:①查询列数一致;②要求每列类型顺序最好一致;③会自动去重(如果不想去重,在union后追加all即可);
例如:从表一查出中国男性,从表二查出美国男性的信息。
select id,cname,csecx from China where csex = '男'
union
select uid,uname,usex from America where usex = 'male';
学习整理于MySQL 基础+高级篇.