问题描述
我正在尝试清除表中所有记录的一列.例如,如果我的表有三列:id
,comment
和likes
-我希望能够清除likes
列.
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|
+-----------------+
|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 | |
+---+-------+-----+
我猜想我将不得不使用MySQL UPDATE
清除likes
值,但是如何遍历所有记录并保持id
和comment
字段相同?
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?
我不想手动更改每条记录.
I don't want to change each record manually.
推荐答案
UPDATE your_table SET likes = NULL
,或者如果您的likes
列不允许NULL
:
or if your likes
column does not allow NULL
:
UPDATE your_table SET likes = ''
默认情况下,某些用于执行数据库查询的SQL工具阻止对所有记录(没有where
子句的查询)进行更新.您可以配置它并删除该保存设置,也可以为所有记录添加一个true
子句,并像这样进行全部更新:
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:
UPDATE your_table
SET likes = NULL
WHERE 1 = 1
如果您与进行比较,那么您还需要IS
运算符.示例:
If you compare with NULL
then you also need the IS
operator. Example:
UPDATE your_table
SET likes = NULL
WHERE likes IS NOT NULL
因为与相同运算符(=
)进行比较 NULL
会返回 UNKNOWN .但是IS
运算符可以处理NULL
.
because comparing NULL
with the equal operator (=
) returns UNKNOWN. But the IS
operator can handle NULL
.
这篇关于表中所有记录的一列的设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!