我有这段代码可以从我当前月份的数据库数据中选择:
SELECT * FROM daily_reports
WHERE username = '$username' and MONTH(date) = MONTH(Now())
order by date DESC
现在我想显示上个月的数据。
列日期是类型日期
答案 0 :(得分:2)
试试这个
SELECT * FROM daily_reports
WHERE username = '$username'
and MONTH(date) = MONTH(DATE_ADD(Now(), INTERVAL -1 MONTH)) order by date DESC
答案 1 :(得分:1)
这应该有效: -
SELECT *
FROM daily_reports
WHERE username = '$username'
and MONTH(date) = MONTH(DATE_ADD(Now(), INTERVAL -1 MONTH))
order by date DESC
答案 2 :(得分:0)
SELECT
*
FROM
daily_reports
WHERE username = '$username'
AND dateColumn BETWEEN SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW();
ORDER BY DATE DESC
答案 3 :(得分:0)
您可以在日期上执行许多操作,如精细手册中所示。
在你的情况下,也许DATE_SUB function最清楚:
SELECT * FROM daily_reports
WHERE username = '$username' AND MONTH(date) = MONTH(DATE_SUB(Now(), INTERVAL 1 MONTH)
ORDER BY date DESC
答案 4 :(得分:0)
date BETWEEN date_format
(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH)