mysql数据查询之子查询

子查询概念:
sub query 查询是在某个查询结果之上进行的 ,一条 select 语句内部包含了另外一条 select 语句。

(1)标量子查询 where 之后写 = ,确定某一个值
select * from student where c_id = ( select id from class where grade = "PM3.1" );

(2)列子查询    where 之后 写 in,是一列的所有值
select * from student where c_id in ( select id from class);

(3)行子查询    两点确定一条直线 ,where之后放两个值,或者直接用max() 或 min()
    • select * from student where age = ? and height = ?;
  • 然后确定最大年龄和最大身高。 

    • select max(age), max(height) from student;


(4)表子查询比较特殊,查询内容放在from之后,查询得到一个表,最好再有个别名
-- 表子查询
select * from ( select * from student order by height desc ) as student group by c_id;


(5)exists子查询,放在where之后

select * from student where exists ( select * from class);

你可能感兴趣的:(mysql)