将mysql列从INT转换为TIMESTAMP

时间:2021-05-20 16:23:19

When I was yound and stupid had little experiance, I decided it would be a good idea, to generate timestamps in PHP and store them in INT column in my MySQL innodb table. Now, when this table has millions of records and needs some date-based queries, it is time to convert this column to TIMESTAMP. How do I do this?

当我和你愚蠢的经验不多时,我决定用PHP生成时间戳并将它们存储在我的MySQL innodb表中的INT列中是个好主意。现在,当此表具有数百万条记录并需要一些基于日期的查询时,是时候将此列转换为TIMESTAMP。我该怎么做呢?

Currenlty, my table looks like this:

Currenlty,我的桌子看起来像这样:

id (INT) | message (TEXT) | date_sent (INT)
---------------------------------------------
1        | hello?         | 1328287526
2        | how are you?   | 1328287456
3        | shut up        | 1328234234
4        | ok             | 1328678978
5        | are you...     | 1328345324

Here are the queries I came up with, to convert date_sent column to TIMESTAMP:

以下是我提出的查询,将da​​te_sent列转换为TIMESTAMP:

-- creating new column of TIMESTAMP type
ALTER TABLE `pm`
  ADD COLUMN `date_sent2` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();

-- assigning value from old INT column to it, in hope that it will be recognized as timestamp
UPDATE `pm` SET `date_sent2` = `date_sent`;

-- dropping the old INT column
ALTER TABLE `pm` DROP COLUMN `date_sent`;

-- changing the name of the column
ALTER TABLE `pm` CHANGE `date_sent2` `date_sent` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();

Everything seems correct to me, but when time comes for the UPDATEpmSETdate_sent2=date_sent;, I get a warning and timestamp value remains empty:

对我来说一切似乎都是正确的,但是当UPDATEpmSETdate_sent2 = date_sent;时,我得到一个警告并且时间戳值保持为空:

+---------+------+--------------------------------------------------+
| Level   | Code | Message                                          |
+---------+------+--------------------------------------------------+
| Warning | 1265 | Data truncated for column 'date_sent2' at row 1  |

What am I doing wrong and is there a way to fix this?

我做错了什么,是否有办法解决这个问题?

1 个解决方案

#1


30  

You're nearly there, use FROM_UNIXTIME() instead of directly copying the value.

你快到了,使用FROM_UNIXTIME()而不是直接复制值。

-- creating new column of TIMESTAMP type
ALTER TABLE `pm`
  ADD COLUMN `date_sent2` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();

-- Use FROM_UNIXTIME() to convert from the INT timestamp to a proper datetime type
-- assigning value from old INT column to it, in hope that it will be recognized as timestamp
UPDATE `pm` SET `date_sent2` = FROM_UNIXTIME(`date_sent`);

-- dropping the old INT column
ALTER TABLE `pm` DROP COLUMN `date_sent`;

-- changing the name of the column
ALTER TABLE `pm` CHANGE `date_sent2` `date_sent` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();

#1


30  

You're nearly there, use FROM_UNIXTIME() instead of directly copying the value.

你快到了,使用FROM_UNIXTIME()而不是直接复制值。

-- creating new column of TIMESTAMP type
ALTER TABLE `pm`
  ADD COLUMN `date_sent2` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();

-- Use FROM_UNIXTIME() to convert from the INT timestamp to a proper datetime type
-- assigning value from old INT column to it, in hope that it will be recognized as timestamp
UPDATE `pm` SET `date_sent2` = FROM_UNIXTIME(`date_sent`);

-- dropping the old INT column
ALTER TABLE `pm` DROP COLUMN `date_sent`;

-- changing the name of the column
ALTER TABLE `pm` CHANGE `date_sent2` `date_sent` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();