2017-10-29 35 views
1

我有两个表user_profiletracked_searchuser_profile表具有用户详细信息,tracked_search跟踪用户进行的搜索。获取所有日期以及来自表格的数据

每当用户进行搜索时,此搜索条目将进入tracked_search表。如果没有搜索到任何特定日期,则tracked_search中不会添加任何内容。

我需要开发一个报告,我需要在每个月的所有日子显示有多少用户进行搜索。

例如

Date  user_count 
2017-10-1 4 
2017-10-2 0 
2017-10-3 0 
2017-10-4 3 
    . 
    . 
2017-10-31 5 

我已经写了下面的查询

SELECT ts.created , count(distinct ts.user_id) FROM tracked_search ts, user_profile u 
WHERE ts.created>=(CURDATE()-INTERVAL 1 MONTH) AND u.id = ts.user_id 
group by ts.created; 

,但我得到

Date user_count 
2017-10-1 4 
2017-10-4 3 

我需要打印所有天的值,如果没有条目是有一个特定的日期应该是零。

我正在使用mysql。

+0

事项一般在应用程序代码中最好的解决,如果这是可用的。 – Strawberry

+0

但我需要发送每一天的数据。在Java中,我将不得不做一个检查,看看哪些日期不存在,然后插入这些日期为零作为清单中的计数。这个过程是可以避免的。 – Suyash

回答

0

你需要编写没有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结果。请考虑我提供的示例。感谢您的帮助,但是, – Suyash

1

顺便说一句,你不需要在user_profile上加入。

如果你有一个dates表的相关日期,这是很容易的:

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; 

因为你很可能没有一个dates表,可能不希望创建和维护一个,你可以使用用于生成动态日期列表的解决方案之一。例如Get a list of dates between two datesHow 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 SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS t0, 
     (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS t1, 
     (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS t2, 
     (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS t3, 
     (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION 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

嗨Wodin感谢您的答复。如果我想按周分组,那该怎么办?要求已经改变。我在下面的链接上创建了一个新的问题https://stackoverflow.com/questions/47013616/mysql-query-to-get-sum-of-user-and-group-by-week – Suyash

+0

@苏伊什我已经回答了你的其他问题问题基于上述答案。 – Wodin

0

我能解决这个使用下面的逻辑希望这会帮助别人的数据显示

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; 
相关问题