MySQL 为表中所有记录的一列设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10364200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:09:43  来源:igfitidea点击:

Setting value for one column of all records in table

mysqlsql

提问by user1199434

I'm trying to clear one column for all records in my table. For example, if my table had three columns: id, comment, and likes- I would like to be able to clear the likescolumn.

我正在尝试清除表中所有记录的一列。例如,如果我的表有三列:idcommentlikes- 我希望能够清除该likes列。

+---+-------+-----+
|id |comment|likes|
+-----------------+
|1  |hi     |3    |
|2  |hello  |12   |
|3  |hey    |1    |
+---+-------+-----+

so that afterwards it would look like this:

以便之后它看起来像这样:

+---+-------+-----+
|id |comment|likes|
+-----------------+
|1  |hi     |     |
|2  |hello  |     |
|3  |hey    |     |
+---+-------+-----+

I'm guessing I would have to use MySQL UPDATEto clear the likesvalue, but how do I iterate through all records and keep the idand commentfields the same?

我猜我必须使用 MySQLUPDATE来清除该likes值,但是如何遍历所有记录并保持idcomment字段相同?

I don't want to change each record manually.

我不想手动更改每条记录。

回答by juergen d

UPDATE your_table SET likes = NULL

or if your likescolumn does not allow NULL:

或者如果您的likes专栏不允许NULL

UPDATE your_table SET likes = ''

Some SQL tools that are used for executing DB queries prevent updates on ALL records (queries without a whereclause) by default. You can configure that and remove that savety setting or you can add a whereclause that is truefor all records and update all anyway like this:

一些用于执行数据库查询的 SQL 工具where默认阻止更新所有记录(没有子句的查询)。您可以配置它并删除该保存设置,或者您可以添加一个适用于所有记录的where子句true并更新所有记录,如下所示:

UPDATE your_table 
SET likes = NULL
WHERE 1 = 1

If you comparewith NULLthen you also need the ISoperator. Example:

如果你比较NULL那么你还需要IS运算符。例子:

UPDATE your_table 
SET likes = NULL
WHERE likes IS NOT NULL

because comparingNULLwith the equal operator (=) returns UNKNOWN. But the ISoperator can handle NULL.

因为比较NULL与所述操作者相等(=)返回UNKNOWN。但IS运营商可以处理NULL