0

我正在修改现有项目。我想将 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 个不同的值?

4

1 回答 1

0

像这样,您将拥有 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 个不同的。

于 2013-06-17T14:49:47.770 回答