mysql group-by,计数为零,使用left-outter连接

nbysray5  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(323)

我有下面的查询,以获得特定日期的用户注册,它的作品发现除了当用户注册计数为零的那一天,我希望它显示为零计数。但是它似乎跳过了这个条目,

SELECT count( * ) AS total, a.Date AS created_dates
                FROM (

                    SELECT (CURDATE() - INTERVAL c.number DAY) AS date
FROM (SELECT singles + tens + hundreds number FROM 
( SELECT 0 singles
UNION ALL SELECT   1 UNION ALL SELECT   2 UNION ALL SELECT   3
UNION ALL SELECT   4 UNION ALL SELECT   5 UNION ALL SELECT   6
UNION ALL SELECT   7 UNION ALL SELECT   8 UNION ALL SELECT   9
) singles JOIN 
(SELECT 0 tens
UNION ALL SELECT  10 UNION ALL SELECT  20 UNION ALL SELECT  30
UNION ALL SELECT  40 UNION ALL SELECT  50 UNION ALL SELECT  60
UNION ALL SELECT  70 UNION ALL SELECT  80 UNION ALL SELECT  90
) tens  JOIN 
(SELECT 0 hundreds
UNION ALL SELECT  100 UNION ALL SELECT  200 UNION ALL SELECT  300
UNION ALL SELECT  400 UNION ALL SELECT  500 UNION ALL SELECT  600
UNION ALL SELECT  700 UNION ALL SELECT  800 UNION ALL SELECT  900
) hundreds
ORDER BY number DESC) c  
WHERE c.number BETWEEN 0 and 364

    ) a
    LEFT OUTER JOIN `users_tp` u
        ON DATE(u.register) = a.Date
WHERE
    ( a.Date  BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE() )
    AND u.origin_reg = 'mobile'
    AND u.step!=5   
GROUP BY a.Date
               ORDER BY a.Date ASC;
r6l8ljro

r6l8ljro1#

问题来自 JOIN :这是一个 LEFT JOIN ,这就是你想要的,但是 WHERE 子句引用被联接的表:因此,它的行为类似于 INNER JOIN .
要解决这个问题,需要将联接表中的条件从 WHERE 条款 ON 合同条款 JOIN .
更改此项:

LEFT OUTER JOIN `users_tp` u
    ON DATE(u.register) = a.Date
WHERE
    ( a.Date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE() )
    AND u.origin_reg = 'mobile'
    AND u.step!=5

收件人:

LEFT OUTER JOIN `users_tp` u
    ON  DATE(u.register) = a.Date
    AND u.origin_reg = 'mobile'  
    AND u.step!=5
WHERE
    a.Date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
3pmvbmvn

3pmvbmvn2#

另一种解决方案:

SELECT count(register ) AS total,a.date AS created_dates from 
(SELECT (CURDATE() - INTERVAL c.number DAY) AS date
FROM (SELECT singles + tens + hundreds number FROM 
( SELECT 0 singles
UNION ALL SELECT   1 UNION ALL SELECT   2 UNION ALL SELECT   3
UNION ALL SELECT   4 UNION ALL SELECT   5 UNION ALL SELECT   6
UNION ALL SELECT   7 UNION ALL SELECT   8 UNION ALL SELECT   9
) singles JOIN 
(SELECT 0 tens
UNION ALL SELECT  10 UNION ALL SELECT  20 UNION ALL SELECT  30
UNION ALL SELECT  40 UNION ALL SELECT  50 UNION ALL SELECT  60
UNION ALL SELECT  70 UNION ALL SELECT  80 UNION ALL SELECT  90
) tens  JOIN 
(SELECT 0 hundreds
UNION ALL SELECT  100 UNION ALL SELECT  200 UNION ALL SELECT  300
UNION ALL SELECT  400 UNION ALL SELECT  500 UNION ALL SELECT  600
UNION ALL SELECT  700 UNION ALL SELECT  800 UNION ALL SELECT  900
) hundreds
ORDER BY number DESC) c  
WHERE c.number BETWEEN 0 and 364)a 

left join 

(SELECT * from users_tp where origin_reg = 'mobile'
    AND step!=5) u

on DATE(u.register) = a.Date
where
(a.Date  BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE() )
GROUP BY a.Date
               ORDER BY a.Date ASC;

相关问题