我想使用条件语句来确定要使用的查询条件集。对任何建议开放。目前我有
Select p.1, p.2, p.3, Count(p.3) ASDF
from table1 t1
left join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.b = t1.b
where
case t1.abc
when 'x' then
t2 in ('1111','2222')
else t3.123 in ('asdf','qwer')
and t2.ad = '123'
and t1.bn = '456'
and t3.mk = '678'
group by p.1, p.2, p.3
我得到"语法不正确"第一个' in'和以下'否则'。我问的可能吗?如果没有,有什么好办法呢?
答案 0 :(得分:0)
这应该可以按你的意愿运作:
SELECT p.1, p.2, p.3, COUNT(p.3) ASDF
FROM table1 t1
LEFT JOIN table2 t2
ON t2.a = t1.a
INNER JOIN table3 t3
ON t3.b = t1.b
WHERE ((t1.abc = 'x' AND t2.somecolumn IN ('1111','2222'))
OR t1.abc <> 'x' AND t3.123 in ('asdf','qwer'))
AND t2.ad = '123'
AND t1.bn = '456'
AND t3.mk = '678'
GROUP BY p.1, p.2, p.3
答案 1 :(得分:0)
Select p.1, p.2, p.3, Count(p.3) ASDF
from table1 t1
left join table2 t2 on t2.a = t1.a
inner join table3 t3 on t3.b = t1.b
where (t1.abc = 'x'AND t2 in ('1111','2222')) OR
(t1.abc <> 'x' AND t3.123 in ('asdf','qwer'))
and t2.ad = '123'
and t1.bn = '456'
and t3.mk = '678'
group by p.1, p.2, p.3