内部查询返回多行

时间:2012-07-04 11:00:23

标签: sql oracle

我在sql developer中使用oracle 11i ..

这是我的疑问:

SELECT itm.stu_no,wm_concat(uif.names) 
FROM personal uif,Coll itm, Dep ihts
WHERE uif.stu_id IN('26',    '29',    '32')   
  and (uif.stu_no,uif.stu_no) in 
  (
       select itm.stu_no,itm.stu_test_no 
       from Col itm,Dep ihts  
       where itm.stu_no = ihts.stu_no
       AND itm.status = 'A'
       AND ihts.status = 'A'
  )
  AND itm.status = 'A'
  and  itm.stu_no=ihts.stu_no and ihts.status = 'A'
  group by itm.stu_no;

内部查询(标记为强文)返回...

stu_no             stu_test_no
-----------------------------------
123                 234

但是完整的查询没有返回任何行。

这是预期的结果..

stu_no              wm_concat(names)
------------------------------------------
123                  James Alex
234                  (null)

有没有办法获得预期的结果?

1 个答案:

答案 0 :(得分:1)

它不起作用,因为您要求uif.stu_no同时为123 234
(123,123)不在((123,234))

(234,234)不在((123,234))

你可以试试这个:

SELECT itm.stu_no,wm_concat(uif.names) 
FROM personal uif,Coll itm, Dep ihts
WHERE uif.stu_id IN('26',    '29',    '32')   
  and uif.stu_no in 
  (
       select itm.stu_no 
       from Col itm,Dep ihts  
       where itm.stu_no = ihts.stu_no
       AND itm.status = 'A'
       AND ihts.status = 'A'
       union all
       select itm.stu_test_no 
       from Col itm,Dep ihts  
       where itm.stu_no = ihts.stu_no
       AND itm.status = 'A'
       AND ihts.status = 'A'
  )
  AND itm.status = 'A'
  and  itm.stu_no=ihts.stu_no and ihts.status = 'A'
  group by itm.stu_no;

但为什么不呢:

SELECT itm.stu_no,wm_concat(uif.names) 
FROM personal uif, Coll itm, Dep ihts
WHERE uif.stu_id IN('26',    '29',    '32')   
  and (uif.stu_no = itm.stu_no OR uif.stu_no = itm.stu_test_no)
  AND itm.status = 'A'
  and itm.stu_no = ihts.stu_no 
  and ihts.status = 'A'
  group by itm.stu_no;