我尝试发一个复杂的sql请求,请帮帮我。
我有这个要求:
SELECT count(id_order) as NbOrders
FROM ORDER
WHERE order.state = 3
and order.source = 1;
但是,我想在同一个请求中也有这个条件的铭文:
WHERE order.state != 3
AND order.source != 1
答案 0 :(得分:1)
尝试做:
SELECT SUM(CASE
WHEN order.state = 3 AND order.source = 1 THEN 1
ELSE 0 END) AS equal,
SUM(CASE
WHEN order.state <> 3 AND order.source <> 1 THEN 1
ELSE 0 END) AS different
FROM order;
这会为您提供第一列中order.state = 3 AND order.source = 1
的计数
第二列中order.state <> 3 AND order.source <> 1
的计数。