麻烦结合两个SELECT语句

时间:2012-11-08 18:17:41

标签: sql select

我相信这有一些简单的解决办法。我基本上没有编写SQL语句的经验,所以我不知道所有的命令,技巧或语法来获得我需要的东西。

我需要结合以下两个SQL SELECT语句:

1

SELECT 
    h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend  
FROM 
    Jhead AS h  
LEFT JOIN 
    jjops AS o ON h.jhjob = o.jajob  
WHERE 
    h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND o.japo = 0  

2:

SELECT 
    h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend, 
    p.hdprcd, p.hdrecd, p.hdseq  
FROM 
    Jhead AS h 
LEFT JOIN 
    jjops AS o ON h.jhjob = o.jajob   
LEFT JOIN 
    hpodt AS p ON h.jhjob = p.hdjob   
WHERE 
    h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND p.hdjob = h.jhjob 
    AND p.hdpo = o.japo 
    AND p.hdseq = o.jaseq

这两个语句都返回我需要的结果。第一个语句基本上返回o.japo = 0的结果(它永远不为null)。第二个返回o.japo不为零的结果,然后我尝试将另一个表连接到数据。我确实尝试在其间插入一个UNION ALL,但我查找的所有联合示例都没有每个select语句的unqiue WHERE语句。


我最初尝试了下面的语句(下面),但是当o.japo = 0时我得到了多行数据并且我无法弄清楚为什么它会拾取额外的行,但我怀疑它是第二个左连接加入不正确/不需要的数据。当o.japo = 0时,所有hpodt(p。)字段都应为null,而是填充来自某处的数据。

SELECT h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend, p.hdprcd, p.hdrecd, p.hdseq 
FROM Jhead AS h 
LEFT JOIN jjops AS o ON h.jhjob = o.jajob 
LEFT JOIN hpodt AS p ON h.jhjob = p.hdjob 
WHERE (h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND o.japcmp = 0 AND p.hdjob = h.jhjob AND o.japo != 0 AND p.hdpo = o.japo AND p.hdseq = o.jaseq AND o.jaopr != 'PT') OR (h.jhpcmp = 0 AND o.jatype = 2 AND o.jacqty < o.jaoqty AND o.japcmp = 0 AND o.japo = 0 AND o.jaopr != 'PT')
ORDER BY h.jhjob, o.jaseq, p.hdrecd

3 个答案:

答案 0 :(得分:3)

在两个陈述之间加上UNION

UNION需要具有相同数量的列,因此您需要做一些额外的工作。

向第一个select语句添加一些空值以覆盖不存在的列。

SELECT 
   -- Common to both tables
   h.jhhold, h.jhjob, h.jhrev, 
   o.jadesc, o.jaoqty, o.jacqty, 
   o.japo, o.javend, 
   -- These columns don't exist here.
   -- include them anyway as NULL
   NULL as hdprcd,
   NULL AS hdrecd, 
   NULL AS hdseq  
FROM Jhead AS h  
LEFT JOIN 
  jjops AS o ON h.jhjob = o.jajob  
WHERE 
    h.jhpcmp = 0 
AND o.jatype = 2 
AND o.jacqty < o.jaoqty 
AND o.japcmp = 0 
AND o.japo = 0
-- The big boy, combining the results of two queries
---
UNION
SELECT 
   h.jhhold, h.jhjob, h.jhrev, 
   o.jadesc, o.jaoqty, o.jacqty, 
   o.japo, o.javend, p.hdprcd, 
   p.hdrecd, p.hdseq  
FROM Jhead AS h 
LEFT JOIN 
   jjops AS o 
ON h.jhjob = o.jajob   
LEFT JOIN 
   hpodt AS p 
ON h.jhjob = p.hdjob   
WHERE 
    h.jhpcmp = 0 
AND o.jatype = 2 
AND o.jacqty < o.jaoqty 
AND o.japcmp = 0 
AND p.hdjob = h.jhjob 
AND p.hdpo = o.japo 
AND p.hdseq = o.jaseq

答案 1 :(得分:1)

再次加入jjops,使用其他别名,然后再加入hpodt

SELECT 
    h.jhhold, h.jhjob, h.jhrev, 
    o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend,
    o2.jadesc, o2.jaoqty, o2.jacqty, o2.japo, o2.javend, 
    p.hdprcd, p.hdrecd, p.hdseq
FROM Jhead AS h  
LEFT JOIN
    jjops AS o ON h.jhjob = o.jajob  
LEFT JOIN
    jjops AS o2 ON h.jhjob = o2.jajob   
LEFT JOIN
    hpodt AS p ON h.jhjob = p.hdjob
    AND p.hdpo = o2.japo
    AND p.hdseq = o2.jaseq
    AND p.hdjob = h.jhjob
WHERE 
    h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND o.japo = 0;

另请注意,我已将部分条件移至ON子句中。如果需要,您可以随时将它们移回到where子句:

...
LEFT JOIN
    hpodt AS p ON h.jhjob = p.hdjob
WHERE 
    h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty 
    AND o.japcmp = 0 
    AND o.japo = 0
    AND p.hdpo = o2.japo
    AND p.hdseq = o2.jaseq
    AND p.hdjob = h.jhjob;

然而,它可能会产生不同的结果 - 最后一个版本不允许LEFT JOIN返回o2中没有p行的行。

答案 2 :(得分:0)

您也可以尝试在原始语句中调整Where子句。

如果你没有选择同一个表,最好避免多次选择。

SELECT h.jhhold, h.jhjob, h.jhrev, o.jadesc, o.jaoqty, o.jacqty, o.japo, o.javend, p.hdprcd, p.hdrecd, p.hdseq 
FROM Jhead AS h 
LEFT JOIN jjops AS o ON h.jhjob = o.jajob   
LEFT JOIN hpodt AS p ON h.jhjob = p.hdjob   
WHERE h.jhpcmp = 0 
    AND o.jatype = 2 
    AND o.jacqty < o.jaoqty
    AND o.japcmp = 0
    AND (o.japo = 0 
        OR (p.hdjob = h.jhjob AND p.hdpo = o.japo AND p.hdseq = o.jaseq 
                  AND o.japo <> 0))