2017-03-07 34 views
0

我有一個事件在下面的架構表:計數與同一日期輸入的所有值 - SQL

ID

名稱(字符串)

created_at(DATETIME)

說明(字符串)

事件應按發生的日期分組並計數

我有這樣的:

SELECT DISTINCT 
    DATE(events.created_at)    AS day, 
    description       AS description, 
    COUNT(*)       AS count 
FROM EVENTS 
WHERE name = 'trained' 
GROUP BY day, events.id 
ORDER BY day ASC`` 

但這總是返回1作爲計數

我想關於COUNT()GROUP BY但他們沒有工作過一些變化

+2

不要按events.id –

+0

它的工作,謝謝! –

回答

1

希望對你有所幫助

SELECT DATE(events.created_at) AS day, 
COUNT(description) AS count 
FROM EVENTS 
WHERE name = 'trained' 
GROUP BY day 
ORDER BY day ASC`` 
0

這工作得很好,我誤解(和因子評分是,我不得不使用events.id的時候,其實我不得不使用events.description作爲必須

SELECT DISTINCT 
     DATE(events.created_at)    AS day, 
     description       AS description, 
     COUNT(*)       AS count 
    FROM EVENTS 
    WHERE name = 'trained' 
    GROUP BY day, events.description 
    ORDER BY day ASC 

但謝謝大家你的幫助,我真的很感謝你的時間