sql note 链接查询

1.链接查询
1.内连接
2.左链接
3.右链接
4.自链接
2.内连接查询
select 字段 from 表1 inner join 表2 on 表1.字段1 = 表2.字段2
e.g.select s.name, c.name from eleves as s inner join classes as c on s.c_id = c.id;
3.左连接
以左表为主根据条件查询右表数据,如果根据条件查询右表数据不存在使用null值填充
select 字段 from 表1 left join 表2 on 表1.字段1 = 表2.字段2;
e.g. select * from students s left join classes c on s.c_id = c.id;
4.右连接
select 字段 from 表1 right join 表2 on 表1.字段1 = 表2.字段2;
5.自连接
左表右表时同一个表,根据查询条件查询两个表的数据;
#执行sql 文件 source
select c.id, c.title, c.pid, p.title from areas as c inner join areas as p on c.pid = p.id where title=‘sfass’;
6.子查询
在一个select 语句中,嵌入了另外一个select语句称为子查询语句,外部的select语句称为主查询。
1.子查询的使用
select * from students where age > (select avg(age) from students);
select name from classes where id in (select cls_id from students where cls_id is not null);
select * from students where (age, height) = (select max(age), max(height) from studens);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值