1

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

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

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 的结果(它永远不会为空)。第二个返回 o.japo 不为零的结果,然后我尝试将另一个表加入数据。我确实尝试在两者之间粘贴一个 UNION ALL,但我查找的所有联合示例都没有每个 select 语句的 unqiue WHERE 语句。

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

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
4

3 回答 3

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
于 2012-11-08T18:27:39.117 回答
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从.o2p

于 2012-11-08T18:42:35.510 回答
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))
于 2012-11-08T18:53:25.580 回答