如何将具有 0000-00-00 00:00:00 值的日期时间设置为 NULL?

2022-01-17 00:00:00 datetime sql-update sql null mysql

我需要更改数据库中的一些值.

I need to change a few values on my DB.

我忘记为表设置可空值,默认设置为 0000-00-00 00:00:00.

I forgot to set nullable to the table and it set to 0000-00-00 00:00:00 by default.

现在我需要将该值转换为 NULL.

Now I need to convert that value in NULL.

字段类型为日期时间.

我该怎么做?

我尝试使用典型的 Update table set field = NULL WHERE field = '0000-00-00 00:00:00'; 但它不起作用.

I try with the typical Update table set field = NULL WHERE field = '0000-00-00 00:00:00'; but it doesn't work.

推荐答案

你需要先让列可以为空:

You need to first make the column nullable:

ALTER TABLE mytable MODIFY COLUMN field DATETIME NULL;

然后更新值:

UPDATE mytable SET field = NULL WHERE field = '0000-00-00 00:00:00';

相关文章