Sql 具有NOT IN的多个WHERE条件
我正在调整存储过程。存储过程正在进行选择,大约有50个WHERE条件。SQL与下面类似。你能告诉我是否有更好的方法来检查这些情况吗Sql 具有NOT IN的多个WHERE条件,sql,where,Sql,Where,我正在调整存储过程。存储过程正在进行选择,大约有50个WHERE条件。SQL与下面类似。你能告诉我是否有更好的方法来检查这些情况吗 SELECT * FROM table A JOIN table B ON A.ID = B.ID WHERE ( ( A.name = 'abc' and B.AID not in ( 111, 222) ) or (A.na
SELECT * FROM table A
JOIN table B
ON A.ID = B.ID
WHERE
(
(
A.name = 'abc'
and B.AID not in ( 111, 222)
)
or
(A.name = 'def'
and B.AID not in ( 222,1113,111,654,199,43,243,643,754244,2434)
)
or
(
A.name = 'etd'
and B.AID not in ( 111,345,54,34,454)
)
or
(
A.name = 'ent'
and B.AID not in ( 111,188,199,1647,128006)
)
or
(
A.name = 'yyy'
and B.AID not in (111,188,1113,1647)
)
or
(
A.name = 'uuu'
and B.AID not in (111,188,1113,1647)
)
or
(
A.name = 'jyf'
and B.AID not in (111,188,1647,344,45444,645)
)
or
(
A.name = 'tut'
and B.AID not in (111,222,1113,1647)
)
)
Name AID
------ ------
abc 111
abc 222
def 222
def 1113
..etc
SELECT * FROM table A
JOIN table B
ON A.ID = B.ID
LEFT JOIN Exclusions e
ON A.name = e.name
and B.AID = e.AID
WHERE e.name IS NULL -- not in exclusion table
SELECT * FROM table A
JOIN table B
ON A.ID = B.ID
WHERE NOT EXISTS(
SELECT null FROM Exclusions e
WHERE A.name = e.name
and B.AID = e.AID
)
嗯,看起来确实很糟糕。如果不知道数据库模式是什么样子,就很难告诉您如何改进。您可以或多或少地将条件存储在表中。然后针对该表加入,而不是制定一系列奇怪的条件。这将更快,并使您能够记录系统。(只需添加一条注释,这一行
'tut',111
是什么意思(以及为什么您不想这样做)),其中一些where子句有一些合并。。。A.name='uuu'和A.name='yyy'具有相同的排除B.AID值列表。否则,我不认为有什么办法可以大大减少这种说法。(如果有人有更好的答案,我会很高兴地感到惊讶)。