子查询
子查询:sub query
,查询是在某个查询结果之上进行的,一条select
语句内部包含了另外一条select
语句。
行子查询
行子查询,返回的结果可以使一行多列或者多行多列。
需求:查询学生表中,年龄最大且身高最高的学生。
思路:
- 先确定数据源,学生表。
select * from student where age = ? and height = ?;
- 然后确定最大年龄和最大身高。
select max(age), max(height) from student;
执行如下 SQL 语句,进行测试:
-- 列子查询
select * from student where
-- 其中,(age, height) 称之为行元素
(age, height) = (select max(age), max(height) from student);
表子查询
表子查询,返回的结果是多行多列二维表(将子查询的结果当做二维表来使用),