更改为存在于 SQL - 使用 DISTINCT
Changing In to Exists in SQL - with DISTINCT
有这个:
code IN
(
SELECT DISTINCT aaa.code
FROM ORGHEADER AS aaa
LEFT JOIN ORGRELATEDPARTY AS bbb
ON aaa.oh_pk = bbb.parent
WHERE aaa.oh_pk NOT IN
(
SELECT fu.parent
FROM ORGRELATEDPARTY fu
WHERE fu.partytype = 'MNG'
)
)
读这一篇:
Changing IN to EXISTS in SQL
试图将其更改为“存在”,但产生了这个但没有用:
code EXISTS
(
SELECT *
FROM ORGHEADER AS aaa
LEFT JOIN ORGRELATEDPARTY AS bbb
ON aaa.oh_pk = bbb.pr_oh_parent
WHERE aaa.oh_pk NOT IN
(
SELECT fu.parent
FROM ORGRELATEDPARTY fu
WHERE fu.pr_partytype = 'MNG'
)
WHERE code = DISTINCT aaa.oh_code
)
The error is 3706: Syntax error: expected something between '=' and 'DISTINCT' keyword.
您已经有冗余代码。
在 IN
子查询中,您正在从 ORGHEADER
中选择一个列,因此 LEFT
连接只会增加噪音,因为它 return 在任何情况下都是来自 ORGHEADER
的所有行.
假设 code
属于别名为 t
的 table,您可以像这样使用 EXISTS
编写代码:
WHERE EXISTS (
SELECT 1
FROM ORGHEADER AS aaa
WHERE aaa.oh_code = t.code
AND aaa.oh_pk NOT IN (
SELECT parent
FROM ORGRELATEDPARTY
WHERE pr_partytype = 'MNG'
)
)
此外,如果 table ORGRELATEDPARTY
的列 parent
可能 return null
,NOT IN
将不起作用。
有这个:
code IN
(
SELECT DISTINCT aaa.code
FROM ORGHEADER AS aaa
LEFT JOIN ORGRELATEDPARTY AS bbb
ON aaa.oh_pk = bbb.parent
WHERE aaa.oh_pk NOT IN
(
SELECT fu.parent
FROM ORGRELATEDPARTY fu
WHERE fu.partytype = 'MNG'
)
)
读这一篇: Changing IN to EXISTS in SQL
试图将其更改为“存在”,但产生了这个但没有用:
code EXISTS
(
SELECT *
FROM ORGHEADER AS aaa
LEFT JOIN ORGRELATEDPARTY AS bbb
ON aaa.oh_pk = bbb.pr_oh_parent
WHERE aaa.oh_pk NOT IN
(
SELECT fu.parent
FROM ORGRELATEDPARTY fu
WHERE fu.pr_partytype = 'MNG'
)
WHERE code = DISTINCT aaa.oh_code
)
The error is 3706: Syntax error: expected something between '=' and 'DISTINCT' keyword.
您已经有冗余代码。
在 IN
子查询中,您正在从 ORGHEADER
中选择一个列,因此 LEFT
连接只会增加噪音,因为它 return 在任何情况下都是来自 ORGHEADER
的所有行.
假设 code
属于别名为 t
的 table,您可以像这样使用 EXISTS
编写代码:
WHERE EXISTS (
SELECT 1
FROM ORGHEADER AS aaa
WHERE aaa.oh_code = t.code
AND aaa.oh_pk NOT IN (
SELECT parent
FROM ORGRELATEDPARTY
WHERE pr_partytype = 'MNG'
)
)
此外,如果 table ORGRELATEDPARTY
的列 parent
可能 return null
,NOT IN
将不起作用。