如何更新具有空值的列
我正在使用 mysql,需要更新具有空值的列。我尝试了很多不同的方法,但我得到的最好的方法是一个空字符串。
有特殊的语法可以做到这一点吗?
I am using mysql and need to update a column with a null value. I have tried this many different ways and the best I have gotten is an empty string.
Is there a special syntax to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
没有特殊语法:
No special syntax:
NULL
是一个特殊值SQL。 因此,要将属性设为 null,请执行以下操作:NULL
is a special value in SQL. So to null a property, do this:使用
IS
代替=
这将解决你的问题
示例语法:
Use
IS
instead of=
This will solve your problem
example syntax:
在上面的答案中,已经建议了许多方法和重复。
我一直在寻找问题的答案,但在这里找不到。
提出上述问题的另一种方式是“更新具有空值的列”
可能是“将列中的所有行更新为空”
在这种情况下,以下工作
is
以及is not
在 mysql 中工作In the above answers, many ways and repetitions have been suggested for the same.
I kept looking for an answer as mentioned is the question but couldn't find here.
Another way to put the above question "update a column with a null value"
could be "UPDATE ALL THE ROWS IN THE COLUMN TO NULL"
In such a situation following works
is
as wellis not
works in mysql请记住查看您的列是否可以为空。您可以使用“如果您的列不能为空”来执行此操作
,当您将值设置为空时,它将成为该列的强制转换值。
这是一个例子
Remember to look if your column can be null. You can do that using
If your column cannot be null, when you set the value to null it will be the cast value to it.
Here a example
对于那些面临类似问题的人,我发现当“模拟”
SET = NULL
查询时,PHPMyAdmin 会抛出错误。这是一个转移注意力的事情......只需运行查询,一切都会好起来的。For those facing a similar issue, I found that when 'simulating' a
SET = NULL
query, PHPMyAdmin would throw an error. It's a red herring.. just run the query and all will be well.如果要使用更新查询设置空值,请将列值设置为 NULL(不带引号)
update tablename set columnname = NULL
但是,如果您直接在 mysql workbench 中编辑字段值,则使用 (Esc + del) 按键将 null 值插入所选列
If you want to set null value using update query set column value to NULL (without quotes)
update tablename set columnname = NULL
However, if you are directly editing field value inside mysql workbench then use (Esc + del) keystroke to insert null value into selected column
使用
is
而不是=
例如:
Select * from table_name where column is null
use
is
instead of=
Eg:
Select * from table_name where column is null
空字符串而不是 true null 的另一个可能原因是该字段是索引或索引的一部分。这发生在我身上:使用 phpMyAdmin,我编辑了一个表中的字段结构,通过选中“Null”复选框,然后点击“NULL”来允许 NULL >保存”按钮。显示“餐桌定价已成功更改”,因此我认为发生了更改 - 但事实并非如此。在执行更新将所有这些字段设置为NULL之后,它们被设置为空字符串,所以我看了一下再次查看表结构,发现该字段的“Null”列设置为“no”。就在那时,我意识到该字段是主键的一部分!
Another possible reason for the empty string, rather than a true null is that the field is an index or is part of an index. This happened to me: using phpMyAdmin, I edited the structure of a field in one of my tables to allow NULLs by checking the "Null" checkbox then hitting the "Save" button. "Table pricing has been altered successfully" was displayed so I assumed that the change happened -- it didn't. After doing an UPDATE to set all of those fields to NULL, they were, instead, set to empty strings, so I took a look at the table structure again and saw that the "Null" column for that field was set to 'no'. That's when I realized that the field was part of the Primary key!
如果您为所有记录设置 NULL 尝试以下操作:
或者仅为特殊记录设置 NULL 使用 WHERE
if you set NULL for all records try this:
OR just set NULL for special records use WHERE
如果您遵循
,那么 name is "" not NULL IN MYSQL 意味着您的查询
SELECT * FROM table WHERE name = NULL 不起作用或让自己失望
if you follow
then name is "" not NULL IN MYSQL means your query
SELECT * FROM table WHERE name = NULL
not work or disappoint yourself在插入时我们可以使用
在更新时我们可以使用
On insert we can use
On update we can use
要在列上设置为 NULL,您需要先让该列允许 NULL,否则肯定会出错。
要编辑列,只需运行以下语法
To set as NULL on a column you need to have the column to allow NULL first, otherwise you'll definitely get error.
To adit the column just run this syntax
我怀疑这里的问题是引号作为字符串值中的文字输入。您可以使用以下方法将这些列设置为空:
您当然应该首先检查这些值是否确实是“”,如下所示:
I suspect the problem here is that quotes were entered as literals in your string value. You can set these columns to null using:
You should of course first check that these values are indeed "" with something like: