表中所有记录的一列的设置值 [英] Setting value for one column of all records in table

查看:72
本文介绍了表中所有记录的一列的设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试清除表中所有记录的一列. 例如,如果我的表有三列:idcommentlikes-我希望能够清除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值,但是如何遍历所有记录并保持idcomment字段相同?

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.

这篇关于表中所有记录的一列的设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆