akunamotata 2009-06-05 16:39
浏览 228
已采纳

数据查询问题

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的值...
问题补充:
还有其他解吗?没有更好的解,我就关闭问题了...
  • 写回答

16条回答 默认 最新

  • chengxing6666 2009-06-05 17:03
    关注

    实在不行就用个性能差的,比较土的:
    select distinct a.user_id

    from table_name a

    where a.course_id =246
    and a.user_id in (select b.user_id

    from table_name b where b.course_id =244 )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(15条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line