建设一个教程视频网站需要什么资质,策划书案例范文,wordpress 下载超链接,石家庄网站建设哪家专业一.联合查询的概念
.对于unio查询,就是把多次查询的结果合并起来,形成一个新的查询果集。
SELECT 字段列表 FROM 表A...
UNION[ALL]
SELECT 字段列表 FROM 表B...#xff0c;
二.将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来
select * from emp where salary
二.将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来
select * from emp where salary5000
select * from emp where age50
union all
将上面的结果集合并起来
对查询的进行去重
union
三.注意点
1.对于联合查询多张表的列数必须保持一致,字段类型也必须保持一致。
2.union all 会将全部的数据直接合并在一起union会对合并之后的数据去重 四.子查询
1.概念
SQL语句中嵌套SELECT语句,称为嵌套语句,又称子查询
SELECT * FROM t1 WHERE column1SELECT column1 FR0M t2;
子查询外部的语句可以是INSERT/UPDATE/DELETE/SELECT的任何一个。 五.标量子查询
子查询返回的结果是单个值(数字,字符串日期等),最简单的形式,这种子查询成为标量子查询。
常用的操作符: 六.案例
1.查询销售部的所有员工信息
a.查询 销售部部门的id
select id from dept where name销售部;b.根据销售部 部门id
select * from emp where dept_id4 select * from emp where dept_id(select id from dept where name销售部) 2.查询在方东白“入职之后的员工信息
a.查询方东白的入职日期
select entrydate from emp where name方东白b.指定入职日期之后入职的员工
select * from emp where entrydateselect entrydate from emp where name方东白; 六.列子查询
子查询返回的结果是一列(可以是多行)这种子查询称为列子查询。
常用的操作符:IN NOT IN ANY SOME,ALL 七.案例
1.查询销售部和市场部的所有员工信息
a.查询销售部和市场部的部门ID
select id from dept where name销售部 or name市场部;
b.根据部门ID查询员工信息
select * from emp where dept_id in(24);
合并:
select * from emp where dept_id in(select id from dept where name销售部 or name市场部); 2.查询比财务部 所有人工资都高的员工信息
selec id from dept where name财务部 select salary from emp where dept_id(selec id from dept where name财务部)
b.比财务部 所有人工资都高的员工信息
select * from emp where salary all(select salary from emp where dept_id(selec id from dept where name财务部))
子查询返回过来的结果都要去满足 3.比研发部其中任意一人工资高的员工信息
a.查询研发部所有人的工作
select salary from emp where dept_id(selec id from dept where name研发部) b.比研发部任意人高的员工工资
selec * from emp where salary any(select salary from emp where dept_id(selec id from dept where name财务部)) 子查询返回的就是单行多列的数据。