Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列[Time]转换为时间戳mysql_Mysql - Fatal编程技术网

将列[Time]转换为时间戳mysql

将列[Time]转换为时间戳mysql,mysql,Mysql,看到这个问题,你可能会发现它是重复的。但我已经研究过了,它不是 问题是我的表中有一列的时间数据类型。现在我想将列转换为时间戳 我也尝试过使用Modify。 询问 ALTER TABLE `mydb`.`temp_table` MODIFY COLUMN `time` TIMESTAMP 但这不起作用。我犯的错误 Error Code: 1292. Incorrect datetime value: '20:00:00' for column 'time' at row 1 Alter tab

看到这个问题,你可能会发现它是重复的。但我已经研究过了,它不是

问题是我的表中有一列的时间数据类型。现在我想将列转换为时间戳

我也尝试过使用Modify。 询问

ALTER TABLE `mydb`.`temp_table` MODIFY COLUMN `time` TIMESTAMP
但这不起作用。我犯的错误

Error Code: 1292. Incorrect datetime value: '20:00:00' for column 'time' at row 1
Alter table change列也不起作用。有什么方法可以转换列时间戳吗

我能看到的最后一个选项是将列转换为VARCHAR,然后更新并添加日期,然后转换为时间戳

更新:

尝试将时间转换为日期时间。没有错误,但数据不正确。更改列后,时间20:00:00更改为2020-00-00:00:00

create table test(`time` time);
insert into test values('10:00:00'), ('20:00:00');
alter table test add ts timestamp null;
update test set ts=`time`;
alter table test drop `time`;
alter table test change ts `time` timestamp not null;

我认为您应该向表中添加另一个时间戳列,并将数据从时间列传输到新的时间戳列。然后您可以删除原始的时间列,并将新列重命名为时间

ALTER TABLE `mydb`.`temp_table` ADD COLUMN `time_stamp` TIMESTAMP;
UPDATE table SET time_stamp = TIMESTAMP(CURDATE(), `time`);
ALTER TABLE `mydb`.`temp_table` 
         DROP COLUMN `time`;         
ALTER TABLE `mydb`.`temp_table` 
         CHANGE COLUMN `time_stamp` `time` TIMESTAMP;

这对我来说效果很好。你期望得到什么样的结果?对我来说,这是一个错误。第1行“时间”列的日期时间值不正确:“20:00:00”。我希望结果可能是1970-01-01 20:00:00。类似的东西我不认为有直接的方法来转换列,但是是的,这很好。只需在workbench中禁用“安全更新”。