Mysql子查询 子查询分为: 列中子查询 单列单行表子查询 必须有别名条件子查询 单行单列 多行单列 下列示例表结构: grade表: result表: student表: subject表:
子查询分为:
列中子查询即
外层查询的结果为其他表的一列数据
示例:
例如查出结果后想要将课程编号换成课程名,就可以使用列中子查询
select a.StudentNo,a.SubjectNo,a.StudentResult,(select b.SubjectName from `subject` b where b.SubjectNo=a.SubjectNo) as 课程名称from result a;
将一张表的查询结果当成一张现有的表,再次与另外一张表进行连接查询
示例:
select tab.*,d.GradeName from (select SubjectName,a.StudentNo,c.GradeId,StudentName,StudentResult from result a LEFT JOIN `subject` b on a.SubjectNo = b.SubjectNo LEFT JOIN student c on a.StudentNo = c.StudentNo) tab LEFT JOIN grade d on tab.GradeId = d.GradeID;
条件子查询用的较多,即将一次查询的查询结果当做另一张表的查询条件,一般这个查询条件要和查询结果字段对应
一般用 = 或者 in来连接
示例:
#查询课程为《高等数学-2 高等数学-1》且分数不小于80分的学生的学号和姓名select * from result a where a.StudentResult>80and a.SubjectNo in (select SubjectNo from `subject` b where b.SubjectName = "高等数学-2" or b.SubjectName = "高等数学-1");
来源地址:https://blog.csdn.net/qq_57480977/article/details/126658379
--结束END--
本文标题: Mysql---子查询的三种查询方式( 表子查询,条件子查询,列中子查询)
本文链接: https://www.lsjlt.com/news/393636.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-13
2024-05-13
2024-05-13
2024-05-13
2024-05-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0