#子查询
#查询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);
数据库--子查询
本文详细介绍了SQL中的子查询使用,包括如何查询年龄大于特定人物、内连接查询、IN查询以及EXISTS查询。通过实例展示了如何在tinfo表和scores表中筛选满足特定条件的数据,如年龄、成绩等。同时,还探讨了子查询的不同形式,如等于、IN和EXISTS在实际查询中的应用。
摘要由CSDN通过智能技术生成