21,886
社区成员
发帖
与我相关
我的任务
分享
WITH tmp as (
-- 查出符合每种条件组合的数量
SELECT count(*) c1, 'c1' as type FROM table where condition = 1
UNION
select count(*) c2, 'c2' as type FROM
(
SELECT * FROM table where condition = 1
) t1 where t1.condition = 2
UNION
SELECT count(*) c3, 'c3' as type FROM
(
SELECT count(*) c2 FROM
(
SELECT * FROM table where condition = 1
) t1 where t1.condition = 2
)t2 WHERE t2.condition = 3
)
SELECT * from (
SELECT * FROM tmp
WHERE true AND
CASE type
WHEN type='c1' && c1>0 THEN
where xxxxx
WHEN type='c2' && c2>0 THEN
where xxxxx
END CASE;
)
太麻烦了 不行就一条一条的写吧 查出来符合1 的 然后再 查同时符合1 和2 的
直至查询中没有结果了 就返回上一次查的结果
-- 符合1
select * from table where condition = 1
-- 1 & 2
select * from table where condition = 1 and condition = 2
-- 类推