union(可以将查询结果集相加)
案例:找出工作岗位是SALESMAN和MANAGER的员工:
第一种:
mysql> select ename,job from emp where job = 'SALESMAN' or job = 'MANAGER'; +--------+----------+ | ename | job | +--------+----------+ | ALLEN | SALESMAN | | WARD | SALESMAN | | JONES | MANAGER | | MARTIN | SALESMAN | | BLAKE | MANAGER | | CLARK | MANAGER | | TURNER | SALESMAN | +--------+----------+
第二种:
mysql> select ename,job from emp where job in('MANAGER','SALESMAN'); +--------+----------+ | ename | job | +--------+----------+ | ALLEN | SALESMAN | | WARD | SALESMAN | | JONES | MANAGER | | MARTIN | SALESMAN | | BLAKE | MANAGER | | CLARK | MANAGER | | TURNER | SALESMAN | +--------+----------+
第三种就是使用union
select ename,job from emp where job = 'MANAGER' union select ename,job from emp where job = 'SALESMAN'; +--------+----------+ | ename | job | +--------+----------+ | JONES | MANAGER | | BLAKE | MANAGER | | CLARK | MANAGER | | ALLEN | SALESMAN | | WARD | SALESMAN | | MARTIN | SALESMAN | | TURNER | SALESMAN | +--------+----------+
union的作用:可以使两张毫不相干的表的查询结果拼接在一起输出,前提是两个查询的列数要相同。
标签:ename,union,job,用法,MANAGER,mysql,SALESMAN,+--------+----------+ From: https://www.cnblogs.com/music-liang/p/17176144.html