gpt4 book ai didi

mysql - 组合字段时获取计数

转载 作者:行者123 更新时间:2023-11-29 08:25:58 25 4
gpt4 key购买 nike

编辑:我更改了表格。请参阅更新后的表格:

在我的示例表中

post            status      
------------- -------------
post1 delivered
post2 undelivered
post3 delayed
post4 delivered
post5 undelivered

是否可以在单个查询中组合获取已送达数量和未送达+延迟数量?

像这样:

post            status      
------------- -------------
delivered 2
undelivered 3

最佳答案

SELECT
SUM(IF(status = 'delivered', 1, 0)) as `delivered_count`,
COUNT(postid) as `all_count`
FROM
table

应该可以正常工作。此帐户只有已交付未交付 状态 - 如果有更多状态,但您仍然只想计算这两个状态,您可以执行以下操作:

SELECT
SUM(IF(status = 'delivered', 1, 0)) as `delivered_count`,
SUM(IF(status = 'delivered' OR status = 'undelivered', 1, 0)) as `all_count`
FROM
table

如果您想要单独计数已送达未送达,您可以使用GROUP BY:

SELECT
COUNT(postid),
status
FROM
table
WHERE
status IN ('delivered', 'undelivered')
GROUP BY
status

关于mysql - 组合字段时获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911531/

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