如何更新具有空值的列

发布于 2024-09-25 23:23:04 字数 84 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

泪是无色的血 2024-10-02 23:23:04

没有特殊语法:

CREATE TABLE your_table (some_id int, your_column varchar(100));

INSERT INTO your_table VALUES (1, 'Hello');

UPDATE your_table
SET    your_column = NULL
WHERE  some_id = 1;

SELECT * FROM your_table WHERE your_column IS NULL;
+---------+-------------+
| some_id | your_column |
+---------+-------------+
|       1 | NULL        |
+---------+-------------+
1 row in set (0.00 sec)

No special syntax:

CREATE TABLE your_table (some_id int, your_column varchar(100));

INSERT INTO your_table VALUES (1, 'Hello');

UPDATE your_table
SET    your_column = NULL
WHERE  some_id = 1;

SELECT * FROM your_table WHERE your_column IS NULL;
+---------+-------------+
| some_id | your_column |
+---------+-------------+
|       1 | NULL        |
+---------+-------------+
1 row in set (0.00 sec)
╰つ倒转 2024-10-02 23:23:04

NULL 是一个特殊值SQL。 因此,要将属性设为 null,请执行以下操作:

UPDATE table SET column = NULL;

NULL is a special value in SQL. So to null a property, do this:

UPDATE table SET column = NULL;
无名指的心愿 2024-10-02 23:23:04

使用 IS 代替 =
这将解决你的问题
示例语法:

UPDATE studentdetails
SET contactnumber = 9098979690
WHERE contactnumber IS NULL;

Use IS instead of =
This will solve your problem
example syntax:

UPDATE studentdetails
SET contactnumber = 9098979690
WHERE contactnumber IS NULL;
度的依靠╰つ 2024-10-02 23:23:04

在上面的答案中,已经建议了许多方法和重复。
我一直在寻找问题的答案,但在这里找不到。

提出上述问题的另一种方式是“更新具有空值的列”
可能是“将列中的所有行更新为空”

在这种情况下,以下工作

update table_name
set field_name = NULL
where field_name is not NULL;

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

update table_name
set field_name = NULL
where field_name is not NULL;

is as well is not works in mysql

空心↖ 2024-10-02 23:23:04

请记住查看您的列是否可以为空。您可以使用“如果您的列不能为空”来执行此操作

mysql> desc my_table;

,当您将值设置为空时,它将成为该列的强制转换值。

这是一个例子

mysql> create table example ( age int not null, name varchar(100) not null );
mysql> insert into example values ( null, "without num" ), ( 2 , null );
mysql> select * from example;
+-----+-------------+
| age | name        |
+-----+-------------+
|   0 | without num |
|   2 |             |
+-----+-------------+
2 rows in set (0.00 sec)

mysql> select * from example where age is null or name is null;
Empty set (0.00 sec)

Remember to look if your column can be null. You can do that using

mysql> desc my_table;

If your column cannot be null, when you set the value to null it will be the cast value to it.

Here a example

mysql> create table example ( age int not null, name varchar(100) not null );
mysql> insert into example values ( null, "without num" ), ( 2 , null );
mysql> select * from example;
+-----+-------------+
| age | name        |
+-----+-------------+
|   0 | without num |
|   2 |             |
+-----+-------------+
2 rows in set (0.00 sec)

mysql> select * from example where age is null or name is null;
Empty set (0.00 sec)
你的往事 2024-10-02 23:23:04

对于那些面临类似问题的人,我发现当“模拟”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.

与风相奔跑 2024-10-02 23:23:04

如果要使用更新查询设置空值,请将列值设置为 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

属性 2024-10-02 23:23:04

使用 is 而不是 =

例如:Select * from table_name where column is null

use is instead of =

Eg: Select * from table_name where column is null

远昼 2024-10-02 23:23:04

空字符串而不是 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!

沧桑㈠ 2024-10-02 23:23:04

如果您为所有记录设置 NULL 尝试以下操作:

UPDATE `table_name` SET `column_you_want_set_null`= NULL

或者仅为特殊记录设置 NULL 使用 WHERE

UPDATE `table_name` SET `column_you_want_set_null`= NULL WHERE `column_name` = 'column_value' 

if you set NULL for all records try this:

UPDATE `table_name` SET `column_you_want_set_null`= NULL

OR just set NULL for special records use WHERE

UPDATE `table_name` SET `column_you_want_set_null`= NULL WHERE `column_name` = 'column_value' 
豆芽 2024-10-02 23:23:04

如果您遵循

UPDATE table SET name = NULL

,那么 name is "" not NULL IN MYSQL 意味着您的查询

SELECT * FROM table WHERE name = NULL 不起作用或让自己失望

if you follow

UPDATE table SET name = NULL

then name is "" not NULL IN MYSQL means your query

SELECT * FROM table WHERE name = NULL not work or disappoint yourself

回忆躺在深渊里 2024-10-02 23:23:04

在插入时我们可以使用

$arrEntity=$entity->toArray();        
    foreach ($arrEntity as $key => $value) {    
        if (trim($entity->$key) == '' && !is_null($entity->$key) && !is_bool($entity->$key)){
        unset($entity->$key);
        }
    }

在更新时我们可以使用

$fields=array();
foreach ($fields as $key => $value) {
        if (trim($value) == '' && !is_null($value) && !is_bool($value)){
            $fields[$key] = null;
        }
    }

On insert we can use

$arrEntity=$entity->toArray();        
    foreach ($arrEntity as $key => $value) {    
        if (trim($entity->$key) == '' && !is_null($entity->$key) && !is_bool($entity->$key)){
        unset($entity->$key);
        }
    }

On update we can use

$fields=array();
foreach ($fields as $key => $value) {
        if (trim($value) == '' && !is_null($value) && !is_bool($value)){
            $fields[$key] = null;
        }
    }
烟酒忠诚 2024-10-02 23:23:04

要在列上设置为 NULL,您需要先让该列允许 NULL,否则肯定会出错。

要编辑列,只需运行以下语法

ALTER TABLE [table_name] CHANGE [column_name] [column_name] VARCHAR(10) NULL DEFAULT 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

ALTER TABLE [table_name] CHANGE [column_name] [column_name] VARCHAR(10) NULL DEFAULT NULL
肤浅与狂妄 2024-10-02 23:23:04

我怀疑这里的问题是引号作为字符串值中的文字输入。您可以使用以下方法将这些列设置为空:

UPDATE table SET col=NULL WHERE length(col)<3;

您当然应该首先检查这些值是否确实是“”,如下所示:

SELECT DISTINCT(col) FROM table WHERE length(col)<3;

I suspect the problem here is that quotes were entered as literals in your string value. You can set these columns to null using:

UPDATE table SET col=NULL WHERE length(col)<3;

You should of course first check that these values are indeed "" with something like:

SELECT DISTINCT(col) FROM table WHERE length(col)<3;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文