将2个sql查询合并到1个查询中

时间:2013-02-14 10:43:56

标签: sql db2

我有2个查询从同一个表中检索记录。我想把所有结果放在一张桌子里。

第一次查询

SELECT ID, NAME, FKNGRP
  FROM EMPINFO      
 WHERE ID = ‘03’
   AND SERIALNO =  ‘N’
   AND (FKNGRP  = 1001 OR FKNGRP = 1002 )

第二次查询

SELECT ID, NAME, FKNGRP
  FROM EMPINFO      
 WHERE ID = ‘04’
   AND SERIALNO =  ‘N’
   AND (FKNGRP  = 1001 OR FKNGRP = 1005 OR FKNGRP = 1009  )

我想结合这两个查询并在DB2中获得结果。

1 个答案:

答案 0 :(得分:2)

SELECT ID, NAME, FKNGRP
FROM EMPINFO      
WHERE 
(
   ID = ‘04’
   and SERIALNO =  ‘N’
   AND FKNGRP in (1001, 1005, 1009)
)
or
(
   ID = ‘03’
   and SERIALNO =  ‘N’
   AND FKNGRP in (1001, 1002)
)