2016-05-23 85 views
1

我想在一個月內統計我數據庫中表格中的條目。例如1月份的總條目。我試過這個,但它沒有解決。在特定月份中計數條目

SELECT MONTH(date1 = '$2015-01-01$') COUNT (*) COUNT from restowlist 
    WHERE MONTH(date1='$2015-01-01$') 

請幫忙。

+0

當然'WHERE MONTH(日期1)= 1' ? – stuartd

+0

'date1'的數據類型是什麼?它是'DATE'還是'DATETIME'? – Barmar

+0

timestamp @Barmar –

回答

1
SELECT 
    COUNT(*) 
FROM 
    restowlist 
WHERE 
    MONTH(yourDateColumn) = 1 AND YEAR(yourDateColumn) = 2015 
1
select count(*) 
from restowlist 
where month(date1) = 1 
and year(date1) = 2015 
1

你可以嘗試

SELECT COUNT(*) 
FROM restowlist 
WHERE DATEPART('year', datecolumn) = 2015 
AND DATEPART('month', datecolumn) = 1 

(道歉,因爲我的語法來自SQL,而不是MySQL的)

1
SELECT COUNT(*) FROM `restowlist` 
WHERE `date1` BETWEEN cast('2016-01-01' as DATE) 
AND cast('2016-01-31' as DATE); 
+0

第二個日期時間應該是'2015-01-31 23:59:59'。否則,你會忽略最後一天的所有行。 – Barmar