我有一个计数头(voucher_credits.voucher_id),我已经使用WHERE语句将它添加到我的报告中,但是我想再次添加它作为另一个列使用不同的标准,怎么能我这样做了?
SELECT vouchers.code, vouchers.credit, count(voucher_credits.voucher_id) as Activated
FROM vouchers, voucher_credits, users
WHERE vouchers.id = voucher_credits.voucher_id
AND voucher_credits.remaining_credit = 0
AND users.id = voucher_credits.user_id AND users.state = 'active'
AND vouchers.is_customer_service = FALSE
GROUP by vouchers.code, vouchers.credit
ORDER by count(voucher_credits.voucher_id) DESC
LIMIT 100
第二列是users.state ='cancel',而没有.remaining_credit = 0
答案 0 :(得分:1)
尝试UNION ALL:
SELECT vouchers.code, vouchers.credit, count(voucher_credits.voucher_id) as Activated
FROM vouchers, voucher_credits, users
WHERE vouchers.id = voucher_credits.voucher_id
AND voucher_credits.remaining_credit = 0
AND users.id = voucher_credits.user_id AND users.state = 'active'
AND vouchers.is_customer_service = FALSE
GROUP by vouchers.code, vouchers.credit
ORDER by count(voucher_credits.voucher_id) DESC
UNION ALL
SELECT vouchers.code, vouchers.credit, count(voucher_credits.voucher_id) as Activated
FROM vouchers, voucher_credits, users
WHERE vouchers.id = voucher_credits.voucher_id
AND users.state = 'cancelled'
AND users.id = voucher_credits.user_id AND users.state = 'active'
AND vouchers.is_customer_service = FALSE
GROUP by vouchers.code, vouchers.credit
ORDER by count(voucher_credits.voucher_id) DESC
答案 1 :(得分:1)
如果所有数据都来自同一行,您可以使用“条件和”计数,这基本上是基于表达式添加0或1的位置:
SELECT ...,
SUM(CASE WHEN voucher_credits.remaining_credit = 0 AND users.state = 'active'
THEN 1 ELSE 0 END) Activated,
SUM(CASE WHEN users.state = 'cancelled' THEN 1 ELSE 0 END) CanceledUser,
FROM ...
...
使用您的查询,这将是:
SELECT vouchers.code, vouchers.credit,
SUM(CASE WHEN voucher_credits.remaining_credit = 0 AND users.state = 'active'
THEN 1 ELSE 0 END) Activated,
SUM(CASE WHEN users.state = 'cancelled' THEN 1 ELSE 0 END) CanceledUser,
FROM vouchers
INNER JOIN voucher_credits ON vouchers.id = voucher_credits.voucher_id
INNER JOIN users ON users.id = voucher_credits.user_id
WHERE vouchers.is_customer_service = FALSE
GROUP by vouchers.code, vouchers.credit
ORDER by Activated DESC
LIMIT 100
答案 2 :(得分:1)
你可以加入桌子两次。我使用了FULL OUTER JOIN
,以防您从2个子查询中获得不同的记录。随意更改连接类型。
select t1.code, t1.credit, t1.Activated, t2.code, t2.credit, t2.Cancelled
from
(SELECT vouchers.code, vouchers.credit, count(voucher_credits.voucher_id) as Activated
FROM vouchers, voucher_credits, users
WHERE vouchers.id = voucher_credits.voucher_id
AND voucher_credits.remaining_credit = 0
AND users.id = voucher_credits.user_id AND users.state = 'active'
AND vouchers.is_customer_service = FALSE
GROUP by vouchers.code, vouchers.credit) t1
FULL OUTER JOIN
(SELECT vouchers.code, vouchers.credit, count(voucher_credits.voucher_id) as Cancelled
FROM vouchers, voucher_credits, users
WHERE vouchers.id = voucher_credits.voucher_id
AND users.state = 'cancelled'
AND users.id = voucher_credits.user_id AND users.state = 'active'
AND vouchers.is_customer_service = FALSE
GROUP by vouchers.code, vouchers.credit) t2
ON t1.code = t2.code and t1.credit = t2.credit