0
0

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 3 years have passed since last update.

SQLiteとMySQLで日時の取得方法が違う

Last updated at Posted at 2021-06-01

MySQLの場合

SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 MONTH); // 現在から1か月後
SELECT DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH); // 現在から1カ月前

SQLiteの場合

※現在日時が2021年6月1日の場合

SELECT DATE('NOW','localtime','1 MONTH'); // 2021-07-01(現在から1か月後)
SELECT DATE('NOW','localtime','-1 MONTH'); // 2021-05-01(現在から1カ月前)

SELECT strftime('%Y/%m/%d',DATE('NOW','localtime','-1 MONTH')); // 2021/05/01
SELECT strftime('%Y年%m月%d日',DATE('NOW','localtime','-1 MONTH')); // "2021年05月01日"


・半年前以降のデータを削除
DELETE FROM テーブル名 WHERE カラム名 < strftime('%Y/%m/%d',DATE('NOW','localtime','-6 MONTH'));
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?