我相信有一些简单的解决方法。我基本上没有编写 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