需求:數據表中若是知足某個條件,返回表中全部數據,不然不返回數據。spa
語法:CASE 表達式是一種通用的條件表達式,相似於其它語言中的 if/else 語句。code
CASE WHEN condition THEN result
[WHEN ...]
[ELSE result] END
例子:test表中若是在'2015-01-02'之後有新插入的數據,返回表中全部數據,若是沒有新插入的數據,返回結果爲空。blog
Table:it
CREATE TABLE test ( test_key integer, create_datetime timestamp without time zone )
Data:io
INSERT INTO test( test_key, create_datetime) VALUES (1, TIMESTAMP'2015-01-01'),(2, TIMESTAMP'2015-02-01');
查詢SQL:class
SELECT test_key, create_datetime FROM test WHERE CASE WHEN (SELECT max(create_datetime) FROM test) > TIMESTAMP'2015-01-02' THEN true END;
返回結果:test
1;"2015-01-01 00:00:00"
2;"2015-02-01 00:00:00"date
查詢SQL:語法
SELECT test_key, create_datetime FROM test WHERE CASE WHEN (SELECT max(create_datetime) FROM test) > TIMESTAMP'2015-02-02' THEN true END;
返回結果爲空。im