Get all Date of month along with data from table

后端 未结 3 972
终归单人心
终归单人心 2020-12-22 00:43

I have two tables user_profile and tracked_search. The user_profile table has user details and tracked_search tracks sear

相关标签:
3条回答
  • 2020-12-22 00:59

    You need to write without AND u.id = ts.user_id.

    SELECT ts.created , count( distinct ts.user_id) FROM tracked_search ts, user_profile u
     WHERE ts.created>=(CURDATE()-INTERVAL 1 MONTH) 
     group by ts.created;
    
    0 讨论(0)
  • 2020-12-22 01:03

    By the way, you don't need the join on user_profile.

    If you have a dates table with the relevant dates, this is pretty easy:

    SELECT dates.day AS `Date`, COUNT(DISTINCT ts.user_id) AS user_count
    FROM dates
    LEFT OUTER JOIN tracked_search AS ts
        ON ts.created = dates.day
    GROUP BY dates.day;
    

    Since you probably don't have a dates table and might not want to create and maintain one, you could use one of the solutions for generating the list of dates on the fly. e.g. Get a list of dates between two dates or How to get list of dates between two dates in mysql select query

    SELECT dates.day AS `Date`, COUNT(DISTINCT ts.user_id) AS user_count
    FROM (
        SELECT ADDDATE('1970-01-01', t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i) AS day
        FROM (SELECT 0 AS i 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) AS t0,
             (SELECT 0 AS i 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) AS t1,
             (SELECT 0 AS i 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) AS t2,
             (SELECT 0 AS i 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) AS t3,
             (SELECT 0 AS i 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) AS t4
    ) AS dates
    LEFT OUTER JOIN tracked_search AS ts
        ON ts.created = dates.day
    WHERE dates.day >= '2017-10-01'
    AND dates.day < '2017-11-01'
    GROUP BY dates.day;
    
    0 讨论(0)
  • 2020-12-22 01:12

    I was able to solve this using the following logic hope it will help someone

    select 
    t1.attempt_date,
    coalesce(SUM(t1.attempt_count+t2.attempt_count), 0) AS attempt_count
    from
    (
      select DATE_FORMAT(a.Date,'%Y/%m/%d') as attempt_date,
      '0' as  attempt_count
      from (
        select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
        from (select 0 as a 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) as a
        cross join (select 0 as a 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) as b
        cross join (select 0 as a 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) as c
      ) a
      where a.Date BETWEEN NOW() - INTERVAL 1 MONTH AND NOW()
    )t1
    left join
    (
      SELECT DATE_FORMAT(ts.created,'%Y/%m/%d') AS attempt_date, 
      count( distinct ts.user_id) AS attempt_count
      FROM tracked_search ts, user_profile u
      WHERE ts.user_id = u.id and
      DATE_SUB(ts.created, INTERVAL 1 DAY) > DATE_SUB(DATE(NOW()), INTERVAL 1 MONTH) 
      GROUP BY DAY(ts.created) DESC
    )t2
    on t2.attempt_date = t1.attempt_date
    group by DAY(t1.attempt_date)
    order by t1.attempt_date desc;
    
    0 讨论(0)
提交回复
热议问题