course_id user_id
------------------------
244 302
244 342
246 342
225 302
248 342
我要把course_id=246和244 都满足的user_id查询出来
请问如何查询?
问题补充:
是都要满足...
in当然不行啦...
问题补充:
select distinct user_id
from table_name
where course_id =246 or course_id =244
和用in有区别么...错...
问题补充:
select distinct user_id
from table_name
where course_id =246
and exists (select user_id
from table_name b where b.user_id=user_id and b.course_id =244 )
错...
问题补充:
我用的是oracle,查询下来上面的补充还是错,和in没区别...
问题补充:
select user_id
from temp4 a
where a.course_id =244
and exists(
select *
from temp4 b
where course_id =246
and b.user_id = a.user_id
);
正确,但是太不灵活...
我已经想到了...
select user_id from(select user_id,count(user_id) num from user_course uc where uc.course_id in (246,244) group by user_id) where num=2;
不知各位如何觉得?
问题补充:
select user_id from(select user_id,count(user_id) num from user_course uc where uc.course_id in (246,244) group by user_id) where num=2;
补充:这个解法有问题,如果course_id 有244,247那不还是错。
xieye (中级程序员) 2009-06-05 采纳为答案
动态的,根据in()里的记录条数,决定num的值...
问题补充:
还有其他解吗?没有更好的解,我就关闭问题了...