select distinct ID, title, takes.course_id
from course join takes
on course.course_id = takes.course_id
where takes.course_id in
(select takes.course_id
from takes
where ID = '10204');
select ID, title, takes.course_id
from course join takes
on course.course_id = takes.course_id
where ID = '10204';
我要查询的过程中ID和一个学生ID为10204取课程名称。第一个结果是5000行,这是不正确的。第二个给出了正确的结果。那么第一个有什么问题呢?为什么两个查询的结果不同?
如果'ID'是一个不明确的列名,你会得到一个错误。 – Thilo 2014-10-09 03:02:07
在第一个查询中(选择takes.course_id from where ID ='10204')这显然是where子句中的take.ID。 第二个查询(从课程连接中选择ID,标题,takes.course_id参加course.course_id = takes.course_id 其中ID ='10204';)在where子句中显然是course.ID。 你疯了吗? – jewelnguyen8 2014-10-09 03:12:00