gpt4 book ai didi

mysql - 即使计数值为0 mysql也选择行

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:55 26 4
gpt4 key购买 nike

select c.id, count(*) as 'Transactions' from voucher v 
right join consumer c on v.consumer_id = c.id
where v.voucher_state in ('REDEEMED', 'EXPIRED') and c.country = 'INDIA'
group by c.id;

预期输出:-

Id 交易

3 0

4 0

6 3

7 9

8 4

9 0

当前输出:-

Id 交易

6 3

7 9

8 4

如何选择计数为 0 的行?谢谢。

最佳答案

您必须使用LEFT JOIN,将consumer 表放在左侧并移动

 v.voucher_state in ('REDEEMED', 'EXPIRED'):

ON 子句:

select c.id, count(v.consumer_id) as 'Transactions' 
from consumer c
left join voucher v on v.consumer_id = c.id on v.voucher_state in ('REDEEMED', 'EXPIRED')
where c.country = 'INDIA'
group by c.id;

以上查询将返回所有 customer.id 满足条件的值

customer.country = 'INDIA'

只有在 voucher 表中有匹配记录的客户

voucher.voucher_state in ('REDEEMED', 'EXPIRED')

将被计算在内。

关于mysql - 即使计数值为0 mysql也选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com