mysql子查询

子查询:语句内部的查询语句就是子查询语句,其父语句不一定是查询语句,子语句一定要用()包起来

按返回值分类分为:一个值、一列、一行、多行多列

一个值:子语句获得一个值后,使用关系运算符> = <,进行运算。

例:获取代课天数相同的老师信息 select t_name,gender from teacher_class where days=(select max(days) from teacher_class)。

一列: 子语句获得一列值后,使用 in not in any等来进行运算。

例:查询所有带过0228班的所有老师信息:select t_name,c_name from teacher_class where t_name in(select t_name from teacher_class where c_name=’php0228’)

一行:子语句获取一行数据后。进行运算。

例:Select * from teacher where (gender,c_name)=(select distinct gender,c_name from teacher_class where t_name=’李白’ and c_name=’php0115’).

多行多列相当于一个表:一般是放到from后面。因为from后面加的是一个表,我们查询出来的是一个结果所以要给结果加一个表明 as 表名。

例:select * from  (select* from teacher) as temp

按位置分:from型和where型。放到from后的就叫from型。放where后就是where型案例如上。

原文地址:https://www.cnblogs.com/dongtong/p/4935001.html