Statistical data by day within a certain period of time, the result of no data is 0

1. Get the number of days in a certain period. 2018-05-03: final time, 10: days
1 SELECT @cdate := date_add(@cdate, INTERVAL - 1 DAY) '时间' FROM (SELECT @cdate := '2018-05-03' FROM tenant_pv LIMIT 10) t1

Results of the:

2. Statistics by day
 1 SELECT
 2     times AS '时间',
 3     count(id) AS '数量'
 4 FROM
 5     (
 6         SELECT
 7             @cdate := date_add(@cdate, INTERVAL - 1 DAY) times
 8         FROM (SELECT @cdate := '2018-05-03' FROM tenant_pv LIMIT 10) t1
 9     ) AS dates
10 LEFT JOIN (
11     SELECT
12         tp.id,
13         tp.createTime
14     FROM
15         tenant_pv tp
16     LEFT JOIN sys_tenant sy ON sy.id = tp.tenantId
17 ) p ON DATE_FORMAT(createTime, '%Y-%m-%d') = dates.times
18 GROUP BY
19     times

Results of the:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215037&siteId=291194637