用于确定 left join on 子句的普遍 SQL if 语句的语法问题
背景故事:我希望使用 if 语句根据条件是否为真进行左连接。但是,如果它是假的,我仍然想加入所述表,但使用相同的列或一组新的列。此查询是为 Pervasive SQL DB 编写的。
该查询相当大,如果没有这个特定问题,它会按预期执行并返回数据集。下面是我当前遇到的问题的快照。
SELECT A.ONUM, B.JN, C.SEQ, C.PN
From Z.OH
LEFT JOIN OL A ON Z.ONUM = A.ONUM
LEFT JOIN JH B ON A.ONUM = B.SNUM AND A.OLNUM = B.SLNUM AND B.CLSD <> 'Y'
LEFT JOIN JO C ON IF(A.LC <> 'ZY', B.JN = C.JN, LEFT(B.C_PO, 6) = C.JN OR B.JN = C.JN) AND C.OP_T NOT IN ('Z','C')
WHERE Z.OT <> 'T' AND C.PN NOT IN (SELECT X.PN FROM JH X WHERE B.JN = X.JN)
再次,非常总结的版本,删除了许多连接/过滤器/选择语句。
我在与 IF 语句连接时遇到问题。如果没有 if 语句,查询将按预期执行。 原始连接为:B.JN = C.JN AND C.OP_T NOT IN ('Z', 'C')
在 PCC 中执行查询时,会在以下位置给出以下语法错误point: "B.JN << ??? >> = C.JN"
我尝试切换到 OR 语句,如下所示,但查询的运行时间使其成功一个不可能的选择。
LEFT JOIN JO C ON
(B.JN = C.JN) OR (A.LC = 'ZY' AND LEFT(B.C_PO, 6) = C.JN)
AND C.OP_T NOT IN ('Z','C')
检查文档,看起来 if 语句上的查询遵循正确的语法......
Back Story: I am looking to do a left join based on if a condition is true with an if statement. However if it is false, I want to still join the said table in, but with the same columns or with a new set of columns. This query is being wrote for a Pervasive SQL DB.
This query is fairly large, without this particular issue it executes and returns the data sets as expected. Below is a snapshot of the issue I am currently running into..
SELECT A.ONUM, B.JN, C.SEQ, C.PN
From Z.OH
LEFT JOIN OL A ON Z.ONUM = A.ONUM
LEFT JOIN JH B ON A.ONUM = B.SNUM AND A.OLNUM = B.SLNUM AND B.CLSD <> 'Y'
LEFT JOIN JO C ON IF(A.LC <> 'ZY', B.JN = C.JN, LEFT(B.C_PO, 6) = C.JN OR B.JN = C.JN) AND C.OP_T NOT IN ('Z','C')
WHERE Z.OT <> 'T' AND C.PN NOT IN (SELECT X.PN FROM JH X WHERE B.JN = X.JN)
Again, very summarized version with lots of joins/filters/select statement removed.
I am running into issues on the join with the IF statement. Without the if statement, the query executes as expected.
The original join being: B.JN = C.JN AND C.OP_T NOT IN ('Z', 'C')
When executing the query in PCC it would give the following syntax error at the following point: "B.JN << ??? >> = C.JN"
I tried switching over to an OR statement as shown below, but the run time of the query made it an impossible choice.
LEFT JOIN JO C ON
(B.JN = C.JN) OR (A.LC = 'ZY' AND LEFT(B.C_PO, 6) = C.JN)
AND C.OP_T NOT IN ('Z','C')
Checking the documentation, it looks like the query on the if statement is following the correct syntax...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是避免 WHERE 子句中的 IF ,并执行以下操作:
Most simple solution would be to avoid the
IF
in the WHERE-clause, and do: