選擇數據我有這樣的代碼從我的數據庫中的數據,從本月中進行選擇:從上個月SQL
SELECT * FROM daily_reports
WHERE username = '$username' and MONTH(date) = MONTH(Now())
order by date DESC
,現在我想從上個月顯示數據。
列日期型日期
選擇數據我有這樣的代碼從我的數據庫中的數據,從本月中進行選擇:從上個月SQL
SELECT * FROM daily_reports
WHERE username = '$username' and MONTH(date) = MONTH(Now())
order by date DESC
,現在我想從上個月顯示數據。
列日期型日期
這應該工作: -
SELECT *
FROM daily_reports
WHERE username = '$username'
and MONTH(date) = MONTH(DATE_ADD(Now(), INTERVAL -1 MONTH))
order by date DESC
SELECT
*
FROM
daily_reports
WHERE username = '$username'
AND dateColumn BETWEEN SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW();
ORDER BY DATE DESC
試試這個
SELECT * FROM daily_reports
WHERE username = '$username'
and MONTH(date) = MONTH(DATE_ADD(Now(), INTERVAL -1 MONTH)) order by date DESC
您可以在日期完成許多操作,如圖所示罰款手冊。
在你的情況,也許DATE_SUB function將是清楚的:
SELECT * FROM daily_reports
WHERE username = '$username' AND MONTH(date) = MONTH(DATE_SUB(Now(), INTERVAL 1 MONTH)
ORDER BY date DESC
date BETWEEN date_format
(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH)
注:這會給你從當月的數據在每年 –
'MONTH(DATE_SUB(NOW(),間隔1 MONTH))' –
我明確地認爲這可以幫助你。 http://stackoverflow.com/questions/1479745/mysql-last-month-date-statement – gpopoteur