WHERE 子句中的嵌套 CASE 语句
Nested CASE statement in the WHERE clause
我正在尝试使用 NESTED CASE 语句(下粗体),根据第一个 CASE 语句,我需要验证内部 where 子句,否则验证其他内部 where 子句。使用嵌套 CASE When 语句时是否采用右下方方法?
SELECT COUNT(*)
FROM <list of table>
WHERE <list of conditions >
AND CASE WHEN cond1 <> 1 or cond2 <>2 or cond3 <> 3
THEN
WHEN ( col1,col2,col3) NOT IN (SELECT col1,col2,col3
FROM table 1,table 2
WHERE <condition1> )
ELSE ( col1,col2,col3,col4) NOT IN (SELECT col1,col2,col3,col4
FROM table 1,table 2
WHERE <condition2> )
END
使用AND
和OR
:
SELECT COUNT(*)
FROM <list of table>
WHERE <list of conditions >
AND ( ( ( cond1 <> 1 OR cond2 <> 2 OR cond3 <> 3 )
AND (col1,col2,col3) NOT IN (SELECT col1,col2,col3
FROM table1
INNER JOIN table2
ON (<condition>)
WHERE <condition1>)
)
OR ( NOT ( cond1 <> 1 OR cond2 <> 2 OR cond3 <> 3 )
AND (col1,col2,col3,col4) NOT IN (SELECT col1,col2,col3,col4
FROM table1
INNER JOIN table2
ON (<condition2>)
WHERE <condition2> )
)
)
注意:不要使用旧式逗号连接,使用现代 ANSI 连接语法。
我正在尝试使用 NESTED CASE 语句(下粗体),根据第一个 CASE 语句,我需要验证内部 where 子句,否则验证其他内部 where 子句。使用嵌套 CASE When 语句时是否采用右下方方法?
SELECT COUNT(*)
FROM <list of table>
WHERE <list of conditions >
AND CASE WHEN cond1 <> 1 or cond2 <>2 or cond3 <> 3
THEN
WHEN ( col1,col2,col3) NOT IN (SELECT col1,col2,col3
FROM table 1,table 2
WHERE <condition1> )
ELSE ( col1,col2,col3,col4) NOT IN (SELECT col1,col2,col3,col4
FROM table 1,table 2
WHERE <condition2> )
END
使用AND
和OR
:
SELECT COUNT(*)
FROM <list of table>
WHERE <list of conditions >
AND ( ( ( cond1 <> 1 OR cond2 <> 2 OR cond3 <> 3 )
AND (col1,col2,col3) NOT IN (SELECT col1,col2,col3
FROM table1
INNER JOIN table2
ON (<condition>)
WHERE <condition1>)
)
OR ( NOT ( cond1 <> 1 OR cond2 <> 2 OR cond3 <> 3 )
AND (col1,col2,col3,col4) NOT IN (SELECT col1,col2,col3,col4
FROM table1
INNER JOIN table2
ON (<condition2>)
WHERE <condition2> )
)
)
注意:不要使用旧式逗号连接,使用现代 ANSI 连接语法。