gpt4 book ai didi

mysql - 带 Mysql 计数的不同 SUM

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

我有一个疑问:

SELECT

t1.name as Name

count ( distinct t2.key ) as Total
SUM ( IF( t2.time = '12:00' , 1 , 0) ) as QttMidDay

FROM t1

LEFT JOIN t2 on t1.key = t2.key

GROUP BY t1.key

问题是,我如何对 QttMidDay 的 2° 参数 SUM 进行“条件计数”?

最佳答案

我猜您可以通过在连接之前聚合来解决您的问题。我最好的猜测是:

SELECT t1.name as Name, t2.Total, t2.QttMidDay 
FROM t1 LEFT JOIN
(SELECT COUNT(DISTINCT t2.key) as Total,
SUM(t2.time = '12:00') as QttMidDay
FROM t2
GROUP BY t2.key
) t2
ON t1.key = t2.key;

我不确定子查询中是否需要 COUNT(DISTINCT)

关于mysql - 带 Mysql 计数的不同 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42297985/

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