简体   繁体   English

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

[英]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. 例如,如果我的表有三列: 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 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值,但是如何遍历所有记录并保持idcomment字段相同?

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设置,也可以添加一个对所有记录都truewhere子句,并像这样进行更新:

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.

相关问题 Mysql如何从一个表中选择列值不是X和Y的所有记录 - Mysql How to select all records from one table where column value is not both X and Y MySQL查找一个表中的记录,而该表没有另一表中的列的值 - MySQL Find records in one table that has no value of a column in another table 将一个表列中的所有记录保存在数组中 - Save all records from one table column in an array 从每个表中删除所有具有特定列值的记录 - Delete all records from each table having a specific column value 设置所有行中一列的值非常慢 - Setting the value of one column of all rows is very slow 如何从一个表中获取 select 条记录,其中第二个表的一列中所有记录的值与第一个表的多个列中的任何一个相匹配? - How to select records from one table where values from all the records in a column of second table match any of the multiple columns of first table? 我在phpmyadmin表中有一个记录列表,我想更改所有记录中一个字段的值 - I have a list of records in a phpmyadmin table, I want to change the value of one field in all the records 从一个表中选择另一个表中存在列值的记录 - Select records from one table where a column value exists in another table 如何通过重复第二个表中的列值来排序一个表中的记录 - How to order records from one table by repetitions of column value by from second table MySQL timediff在表中选定记录的一列上 - MySQL timediff on one column for selected records in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM