gpt4 book ai didi

MySQL : Different behaviors depending on WHERE result

转载 作者:行者123 更新时间:2023-11-30 01:33:53 25 4
gpt4 key购买 nike

我正在修改现有项目。我想将 3 个 mysql 请求合并为一个。

这3个请求具有相同的选定数据,只是WHERE发生变化。这是示例请求之一:

SELECT COUNT(seg.my_seg1) FROM (
SELECT COUNT(DISTINCT cp.conference_id) as my_seg1 FROM A.Account a
INNER JOIN A.ConferenceParticipant cp ON a.account_id = cp.user_id
INNER JOIN A.Conference cf ON cf.id = cp.conference_id
WHERE cf.`status` = 0
AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= cf.creation_timestamp
GROUP BY a.account_id) as seg
WHERE seg.my_seg1 >= 30

其他 2 个请求完全相同,除了:

WHERE seg.my_seg1 >= 11 AND seg.my_seg1 <= 30;

和:

WHERE seg.my_seg1 >= 30;

所以我的问题是如何根据同一请求中的 WHERE 结果获得 3 个不同的值?

最佳答案

这样您将拥有 3 个虚拟列:

SELECT 
COUNT(IF(seg.my_seg1 >= 30, 1, 0)) AS res1,
COUNT(IF(seg.my_seg1 >= 11 AND seg.my_seg1 < 30, 1, 0)) AS res2
FROM (
SELECT COUNT(DISTINCT cp.conference_id) as my_seg1
FROM A.Account a
JOIN A.ConferenceParticipant cp ON a.account_id = cp.user_id
JOIN A.Conference cf ON cf.id = cp.conference_id
WHERE
cf.`status` = 0
AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= cf.creation_timestamp
GROUP BY a.account_id
) AS seg

但是你必须修改你的过滤器,你说的是 3 个,但我只看到 2 个不同的。

关于MySQL : Different behaviors depending on WHERE result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17149742/

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