gpt4 book ai didi

mysql - sql中按状态计数列

转载 作者:行者123 更新时间:2023-11-29 17:57:02 24 4
gpt4 key购买 nike

我需要 SQL 表中的寄存器总数。
我有这样的状态:

SELECT 
COUNT(CASE WHEN status = 'WAITING_MEASUREMENT' THEN 1 ELSE NULL END) as '1',
COUNT(CASE WHEN status = 'WAITING_WEIGHMENT' THEN 1 ELSE NULL END) as '2',
COUNT(CASE WHEN status = 'WAITING_DATA_ENTRY' THEN 1 ELSE NULL END) as '3',
COUNT(CASE WHEN status = 'WAITING_PICTURES' THEN 1 ELSE NULL END) as '4'
FROM product;

但是当我运行查询时,我在所有列中都有相同的结果。查询有什么问题?

最佳答案

您应该求和而不是计数,并使用 0 而不是 null,计数只会计算所有记录。

 SELECT 
SUM(CASE WHEN status = 'WAITING_MEASUREMENT' THEN 1 ELSE 0 END) as '1',
SUM(CASE WHEN status = 'WAITING_WEIGHMENT' THEN 1 ELSE 0 END) as '2',
SUM(CASE WHEN status = 'WAITING_DATA_ENTRY' THEN 1 ELSE 0 END) as '3',
SUM(CASE WHEN status = 'WAITING_PICTURES' THEN 1 ELSE 0 END) as '4'
FROM product;

关于mysql - sql中按状态计数列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48712928/

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