SQL经典50查询语句案例_6:
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名:
SELECT
st.SId,
st.StuName
FROM(
SELECT t1.SId AS SId
FROM
(SELECT s1.SId FROM sc s1 WHERE s1.CId = '001')t1,
(SELECT s2.SId FROM sc s2 WHERE s2.CId = '002')t2
WHERE
t1.SId = t2.SId
)stu, student st
WHERE
stu.SId = st.SId
在MySQL命令行中的运行情况:
总结:采用分步查询的思路,先是查询到既包含001课程又包含002课程的学号(内嵌select from),然后在根据得到的学号去student表查询学号与姓名。