Mysql 子查询

子查询:查询语句中包含一个或者多个其他的查询语句,内部的查询语句称之为子查询

条件中使用
如果子查询的结果是多条记录(单列),作为条件使用时,应该用in或者not in

--查询各部门最高薪资的员工信息
select * from tb_emp
where salary in(
select max(salary)
from tb_emp
group by dept_id
);

如果子查询的结果时单个,可以使用= , > ,< ,>=,<=,!=

连接中使用
join

创建视图
视图:是一种查询语句,用于复杂sql语句的封装。
create view 视图名 as select语句

create view v_all_emp as select a.*,b.name department ,b.tel dept_tel
from tb_emp a
join tb_dept b on (a.dept_id=b.id);

–查询 练习电话未002的部门下的所有员工
select * from v all emp where dept_tel =‘002’;

删除视图:
drop view 视图名;

查询视图:
select table_nam ,table _type
from information_schema.tables
where table_schema=‘pdb’
and table_type=‘VIEW’;

创建表使用
create table tb_emp2
select * from tb_emp1;

create table tb_tb_emp3 select * from tb_emp where id=null;

插入数据使用
insert into 表名
子查询语句;

将tb_emp表中第一个部门的员工插入到tb_emp3表中
insert into tb_emp3
select * from tb_emp where dept_id=1

联合查询
子查询1
union[all]
子查询2

union【all】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值