2017-10-29 60 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; 
相關問題