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

MySQLでdatetime型の演算をする

Last updated at Posted at 2020-03-15

以下のようなテーブルがある.

MariaDB [staging]> desc guests;
+---------------+----------------------+------+-----+---------+----------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+----------------------+------+-----+---------+----------------+
| id            | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| department_id | smallint(5) unsigned | YES  | MUL | NULL    |                |
| user_id       | varchar(10)          | YES  |     | NULL    |                |
| comment       | varchar(255)         | YES  |     | NULL    |                |
| entered_at    | datetime             | NO   |     | NULL    |                |
| updated_at    | datetime             | NO   |     | NULL    |                |
| exited_at     | datetime             | YES  |     | NULL    |                |
+---------------+----------------------+------+-----+---------+----------------+
7 rows in set (0.036 sec)

現状では,JSTでdatetime型のカラムに記録されている.

MariaDB [staging]> select * from guests;
+----+---------------+----------+-----------------------------+---------------------+---------------------+---------------------+
| id | department_id | user_id  | comment                     | entered_at          | updated_at          | exited_at           |
+----+---------------+----------+-----------------------------+---------------------+---------------------+---------------------+
|  3 |             1 | C0117123 | これはテストです〜          | 2019-08-08 02:41:20 | 2019-08-08 20:41:20 | 2020-03-02 23:00:09 |

ここでは entered_at カラムの時刻をUTCへ修正する.

UPDATE guests SET entered_at=entered_at - INTERVAL 9 HOUR

修正されたことが確認できる.

MariaDB [staging]> select * from guests;
+----+---------------+----------+-----------------------------+---------------------+---------------------+---------------------+
| id | department_id | user_id  | comment                     | entered_at          | updated_at          | exited_at           |
+----+---------------+----------+-----------------------------+---------------------+---------------------+---------------------+
|  3 |             1 | C0117123 | これはテストです〜          | 2019-08-07 17:41:20 | 2019-08-08 20:41:20 | 2020-03-02 23:00:09 |
1
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
1
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?