为什么我要在表中存入time_t?
因为这个数字唯一,能够方便的转换。
如果要保存时间日期,显示为2024-10-10,涉及到时区转换问题。我显示的到底是谁?
那么select语句中,怎么转换呢?
- 设置时区
SET global time_zone='+08:00';
SET time_zone='+08:00';
- 把time_t字段转为datetime
转换为字串(0时区)
mysql> select id, FROM_UNIXTIME(start_time) timestr from target_table;
+----+---------------------+
| id | timestr |
+----+---------------------+
| 1 | 2024-10-12 13:44:24 |
| 2 | 2024-10-12 13:44:24 |
| 3 | 2024-10-12 13:44:24 |
| 4 | 2024-10-12 13:44:24 |
| 5 | 2024-10-12 13:44:24 |
| 6 | 2024-10-12 13:44:24 |
| 7 | 2024-10-12 13:44:24 |
+----+---------------------+
7 rows in set (0.00 sec)转换为小时
mysql> select id, HOUR(FROM_UNIXTIME(start_time)) hourstr from radar_target_table;
+----+---------+
| id | hourstr |
+----+---------+
| 1 | 13 |
| 2 | 13 |
| 3 | 13 |
| 4 | 13 |
| 5 | 13 |
| 6 | 13 |
| 7 | 13 |
+----+---------+
7 rows in set (0.00 sec)
- datetime字段转为time_t
select UNIX_TIMESTAMP(time) valuetime from target_table