数据库--子查询

本文详细介绍了SQL中的子查询使用,包括如何查询年龄大于特定人物、内连接查询、IN查询以及EXISTS查询。通过实例展示了如何在tinfo表和scores表中筛选满足特定条件的数据,如年龄、成绩等。同时,还探讨了子查询的不同形式,如等于、IN和EXISTS在实际查询中的应用。
摘要由CSDN通过智能技术生成
#子查询
#查询tinfo表中比张三年龄大的人的信息(括号中的语句就是子查询语句,括号前的是父语句)
#注:父语句中的条件字段(where后的字段)与子语句查询字段(select后的字段)相同
select * from tinfo where age>(select age from tinfo where name='张三');
#查询数学成绩等于80的人的信息
#1.内连接
select t.id,t.name,t.sex,t.age,s.grade from score s 
inner join tinfo t on s.tid=t.id 
inner join course c on s.cid=c.id 
where s.grade=80 and c.name='数学';
#2.子查询
select * from tinfo where id=(select s.tid from score s inner join course c 
on s.cid=c.id where s.grade=80 and c.name='数学');

#子查询--in查询:与=相同,但in子查询的结果有多条,=只返回一条结果,同理not in与!=相同
#查询tinfo表中比张三年龄相同的人的信息(括号中的语句就是子查询语句)
select * from tinfo where age in(select age from tinfo where name='张三');

#子查询--exists查询:只要exist后的子查询语句成立就会执行父语句
#查询数学成绩高于80的人的信息
select * from tinfo where exists(select grade from score where grade>80);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值