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
Setting value for one column of all records in table
提问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 likes
column.
我正在尝试清除表中所有记录的一列。例如,如果我的表有三列:id
、comment
和likes
- 我希望能够清除该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 UPDATE
to clear the likes
value, but how do I iterate through all records and keep the id
and comment
fields the same?
我猜我必须使用 MySQLUPDATE
来清除该likes
值,但是如何遍历所有记录并保持id
和comment
字段相同?
I don't want to change each record manually.
我不想手动更改每条记录。
回答by juergen d
UPDATE your_table SET likes = NULL
or if your likes
column 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 where
clause) by default. You can configure that and remove that savety setting or you can add a where
clause that is true
for all records and update all anyway like this:
一些用于执行数据库查询的 SQL 工具where
默认阻止更新所有记录(没有子句的查询)。您可以配置它并删除该保存设置,或者您可以添加一个适用于所有记录的where
子句true
并更新所有记录,如下所示:
UPDATE your_table
SET likes = NULL
WHERE 1 = 1
If you comparewith NULL
then you also need the IS
operator. Example:
如果你比较,NULL
那么你还需要IS
运算符。例子:
UPDATE your_table
SET likes = NULL
WHERE likes IS NOT NULL
because comparingNULL
with the equal operator (=
) returns UNKNOWN. But the IS
operator can handle NULL
.
因为比较NULL
与所述操作者相等(=
)返回UNKNOWN。但IS
运营商可以处理NULL
。