3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLで先月のデータを絞り込む

Posted at

結論

SELECT * 
FROM books
WHERE buy_time >= DATE_ADD(DATE_ADD(LAST_DAY(NOW()), INTERVAL 1 DAY), INTERVAL -2 MONTH)
  AND buy_time <  DATE_ADD(DATE_ADD(LAST_DAY(NOW()), INTERVAL 1 DAY), INTERVAL -1 MONTH);

関数について

NOW() … 現時点の日時を取得
LAST_DAY(日時) :最終日を取得
DATE_ADD(日時, INTERVAL 3 DAY):3日後の日時を取得

コピペしてためしてみる用

結論に書いてあるコードはテーブルがないと動かないので、MySQLで実行すれば動くコードを以下に貼っておきます

SELECT NOW() AS 現時刻, LAST_DAY(NOW()) AS 月末, DATE_ADD(NOW(), INTERVAL 3 DAY) AS 3日後

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?