[英]Setting value for one column of all records in table
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? 我猜我将不得不使用MySQL
UPDATE
来清除likes
值,但是如何遍历所有记录并保持id
和comment
字段相同?
I don't want to change each record manually. 我不想手动更改每条记录。
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. 默认情况下,某些用于执行数据库查询的SQL工具阻止对所有记录(没有
where
子句的查询)进行更新。 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: 您可以配置它并删除该savety设置,也可以添加一个对所有记录都
true
的where
子句,并像这样进行更新:
UPDATE your_table
SET likes = NULL
WHERE 1 = 1
If you compare with NULL
then you also need the IS
operator. 如果与
NULL
比较 ,则还需要IS
运算符。 Example: 例:
UPDATE your_table
SET likes = NULL
WHERE likes IS NOT NULL
because comparing NULL
with the equal operator ( =
) returns UNKNOWN . 因为将
NULL
与等于运算符( =
)比较将返回UNKNOWN 。 But the IS
operator can handle NULL
. 但是
IS
运算符可以处理NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.