
mysql 中如何将 int 时间戳转换为 timestamp
在 mysql 表中,需要将 int 类型的时间戳转换为 timestamp 类型。
表
| id (bigint) | created_at(int) | created_time (timestamp) |
|---|---|---|
| 1 | 1628075987 | |
| 2 | 1628075739 |
sql 解决方案
无需通过编程语言读写,可以通过 sql 直接转换:
update table_name set created_time = from_unixtime(created_at);
示例
执行以下查询将 int 时间戳转换为 timestamp:
select from_unixtime(1156219870);
结果
2006-09-20 04:04:30
生成的 timestamp 列如下所示:
created_time 2021-08-04 17:06:27 2021-08-04 17:02:19










