2014-12-01 87 views
0

我有一個表與一些事件與時間戳一起作爲DATETIME。 現在我想對我的數據進行統計,例如每天有多少事件...在某些情況下,我每天都沒有事件發生,當然我沒有得到那些沒有外部條目的統計數據。MySQL的DATETIME排序虛擬日期表

| id | DATE   | COUNT  | 
| 1 | 2014-09-06  | 1   | 
| 2 | 2014-09-07  | 8   | 
| 3 | 2014-09-10  | 2   | 
| 4 | 2014-09-14  | 78   | 

因此,我寫了一個小腳本誰產生了一個查詢來解決這個問題。它會根據我想知道的日子生成一個虛擬表,並與我的事件表一起執行LEFT OUTER JOIN。 所以我會得到沒有差距的所有日期!查詢看起來是這樣的例如爲:

SELECT DATE_FORMAT(d.date, '%d.%m.%Y') as datum, COUNT(l.id) as anzahl 
FROM 
( 
SELECT STR_TO_DATE('25.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('26.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('27.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('28.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('29.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('30.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('01.12.2014', '%d.%m.%Y') as date 
) as d 
LEFT OUTER JOIN events l ON d.date = DATE(l.date) 
GROUP BY datum 
ORDER BY datum DESC 

此查詢的工作絕對完美的,我也有我的統計沒有數據的日期。

但現在來了我真正的問題:排序不工作!我有一些奇怪的輸出。我不知道問題是什麼。輸出如下:

| DATE   | COUNT | 
| 31.10.2014 | 0  | 
| 30.11.2014 | 5  | 
| 30.10.2014 | 0  | 
| 29.11.2014 | 0  | 
| 29.10.2014 | 0  | 
| 28.11.2014 | 0  | 
| 28.10.2014 | 0  | 
| 27.11.2014 | 0  | 
| 27.10.2014 | 0  | 
| 26.11.2014 | 0  | 
| 26.10.2014 | 0  | 
| 25.11.2014 | 1  | 
| 25.10.2014 | 0  | 
| 24.11.2014 | 1  | 
| 24.10.2014 | 0  | 
| 23.11.2014 | 0  | 
| 23.10.2014 | 0  | 
| 22.11.2014 | 0  | 
| 22.10.2014 | 0  | 
| 21.11.2014 | 1  | 
| 21.10.2014 | 0  | 
| 20.11.2014 | 0  | 
| 20.10.2014 | 0  | 
| 19.11.2014 | 2  | 
| 19.10.2014 | 0  | 
| 18.11.2014 | 0  | 
| 18.10.2014 | 0  | 
| 17.11.2014 | 0  | 
| 17.10.2014 | 0  | 
| 16.11.2014 | 0  | 

那麼,我的查詢有什麼問題?我有意識地使用函數STR_TO_DATE我有一個「真正的」日期格式。通常排序應該與它一起工作,不是嗎?

+0

如果是我,我不會觸及日期格式化,直到我回到客戶端 – Strawberry 2014-12-01 23:35:36

回答

0

問題是你的眼睛所看到的日期,但在你的quesry他們串所以他們正確排序爲字符串與個月第一。嘗試使用anotehr字段進行排序並將其用於排序。

SELECT DATE_FORMAT(d.date, '%d.%m.%Y') as datum, DATE_FORMAT(d.date, '%Y.%m.%d') as sortdatum, COUNT(l.id) as anzahl 
FROM 
( 
SELECT STR_TO_DATE('25.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('26.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('27.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('28.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('29.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('30.11.2014', '%d.%m.%Y') as date UNION ALL 
SELECT STR_TO_DATE('01.12.2014', '%d.%m.%Y') as date 
) as d 
LEFT OUTER JOIN events l ON d.date = DATE(l.date) 
GROUP BY datum 
ORDER BY sortdatum DESC 
+0

THX爲您提供幫助。我將你的解決方案與sunysen的想法結合起來。現在我使用「UNIX_TIMESTAMP(d.date)作爲sortdatum」進行排序。 – Denis 2014-12-02 00:17:46

0
please it **unix_timestamp** 

SELECT DATE_FORMAT(d.date, '%d.%m.%Y') as datum, COUNT(l.id) as anzahl 
    FROM 
    ( 
    SELECT STR_TO_DATE('25.11.2014', '%d.%m.%Y') as date UNION ALL 
    SELECT STR_TO_DATE('26.11.2014', '%d.%m.%Y') as date UNION ALL 
    SELECT STR_TO_DATE('27.11.2014', '%d.%m.%Y') as date UNION ALL 
    SELECT STR_TO_DATE('28.11.2014', '%d.%m.%Y') as date UNION ALL 
    SELECT STR_TO_DATE('29.11.2014', '%d.%m.%Y') as date UNION ALL 
    SELECT STR_TO_DATE('30.11.2014', '%d.%m.%Y') as date UNION ALL 
    SELECT STR_TO_DATE('01.12.2014', '%d.%m.%Y') as date 
    ) as d 
    LEFT OUTER JOIN events l ON unix_timestamp(d.date) = unix_timestamp(l.date) 
    GROUP BY datum 
    ORDER BY datum DESC