我有一个SQLite表,我可以做三个不同的查询,比如这个伪SQL ...
SELECT COUNT(*) FROM myTable WHERE day=today AND colA = "xxx";
SELECT COUNT(*) FROM myTable WHERE day=today AND colB = "yyy";
SELECT COUNT(*) FROM myTable WHERE day=today AND colC = "zzz";
我基本上想知道今天是否有任何匹配(如果是,哪一列)。有更好的方法吗?
答案 0 :(得分:1)
SELECT
SUM(O.A) A,
SUM(O.B) B,
SUM(O.C) C
FROM (
SELECT
CASE WHEN T.colA = 'xxx' THEN 1 ELSE 0 END A,
CASE WHEN T.colB = 'yyy' THEN 1 ELSE 0 END B,
CASE WHEN T.colC = 'zzz' THEN 1 ELSE 0 END C
FROM
myTable T
WHERE
T.day = today
) O
;