我只是想知道是否可以在WHERE子句中准确地使用这种形式的CASE语句......
SELECT *
FROM TABLEA
WHERE date between '2014-02-01' and '2014-02-28' and
CASE
WHEN date>'2014-02-28' THEN (SELECT FROM TABLEC WHERE...)
ELSE (SELECT FROM TABLE B WHERE...)
END
谢谢!
答案 0 :(得分:3)
是的,在以下情况下可以这样做:
=
或>
case
语句返回标量值。具有一列和一个值的行与标量值“等效”。因此,允许以下内容:
where col = (CASE WHEN date > '2014-02-28' THEN (SELECT max(col2) FROM TABLEC WHERE...)
ELSE (SELECT min(col3) FROM TABLE B WHERE...)
END)
但是,您可能希望执行条件in
语句。避开case
:
where date > '2014-02-28' and col in (SELECT max(col2) FROM TABLEC WHERE...) or
date <= '2014-02-28' and col in (SELECT min(col3) FROM TABLE B WHERE...)