您的位置:首页 > 数据库 > MySQL

mysql子查询

2015-11-04 09:11 316 查看
子查询:语句内部的查询语句就是子查询语句,其父语句不一定是查询语句,子语句一定要用()包起来

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

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

例:获取代课天数相同的老师信息 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型案例如上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: