0
点赞
收藏
分享

微信扫一扫

php curl 开启与实例教程

忍禁 2023-06-23 阅读 79

前言

 目录

前言

 目录

一、子查询

1. 子查询的概念

2. 子查询语法格式 

2.1 根据子查询结果不同可以分为:

2.2 根据子查询位置分为:

2.3 标量子查询概念

2.4 标量子查询练习 

2.3 列子查询概念

​2.4 列子查询练习

2.5 行子查询概念

2.6 行子查询练习 

2.7 表子查询概念

二、总结


一、子查询

1. 子查询的概念

2. 子查询语法格式 

select * from 表1  where column1=(select column1 from 表2);

子查询外部的语句可以是insert/update/delete/select 的任何一个。 

2.1 根据子查询结果不同可以分为:

2.2 根据子查询位置分为:

select id from dept where name='市场部';

2.3 标量子查询概念

2.4 标量子查询练习 

2.4.1 查询 "市场部" 的所有员工的信息(使用的是上期的表结构)

a.查询"市场部"部门id

select id from dept where name='市场部';

b.根据市场部门id查询员工信息

select * from emp where dept_id=4;

a.b.合二为一

select * from emp where dept_id=(select id from dept where name='市场部');

2.4.2 查询杜甫入职之后的员工信息

a.查询杜甫的入职日期

select entrydate from emp where name='杜甫';

b.查询指定日期之后入职的员工信息

select * from emp where entrydate>'0120-01-01';

合二为一

select * from emp where entrydate>(select entrydate from emp where name='杜甫');

2.3 列子查询概念

2.4 列子查询练习

2.4.1 查询研发部和市场部所有员工的信息

a.查询研发部和市场部的部门ID

select id from dept where name='市场部'or name='研发部';

b.根据部门id查询员工信息

select * from emp where dept_idin(2,3);

合二为一

select *
from emp
where dept_id in (select id from dept where name = '市场部' or name = '研发部');

2.4.2 查询比市场部所有人工资都高的员工信息

a.查询所有市场部人员工资

select salary from emp where dept_id=(select id from dept where name='市场部');

b.查询比市场部所有员工工资都高的员工信息

select * from empwhere salary>all

(select salary from emp where dept_id=(select id from dept where name='市场部'));

2.4.3 查询比市场部其中一人工资高的员工信息

select * from emp where salary>any

(select salary from emp where dept_id=(select id from dept where name='市场部'));

2.5 行子查询概念

2.6 行子查询练习 

 查询与杜甫的薪资及直属领导相同的员工信息

2.6.1 行子查询

select *
from emp
where (salary, mangagerid) = (select salary, mangagerid from emp where name = '杜甫');

2.7 表子查询概念

2.7.1 查询与李白杜甫置位相同和薪资相同的员工信息

select *
from emp
where (job, mangagerid) in (select salary, mangagerid
                            from emp
                            where name = '李白'

                               or name = '杜甫');

2.7.2 查询入职日期是’100-01-01‘之后员工的日期信息,其部门信息

a.入职日期是‘100-01-01’之后的信息

select * from emp where entrydate>'100-01-01';

b.查询这部分员工,对应的部门信息 

select e.*,d.* from(select * from emp where entrydate>'100-01-01') e
left join dept on e.dept_id=d.id;

二、总结

 

举报

相关推荐

0 条评论