如何用空值更新列

我正在使用MySQL,需要更新一个空值的列。 我已经尝试了很多不同的方法,我得到的最好的是一个空string。

有没有特殊的语法来做到这一点?

没有特别的语法:

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) 

NULL是SQL中的一个特殊值。 所以要清空一个属性,请执行以下操作:

 UPDATE table SET column = NULL; 

请记住查看您的列是否可以为空。 你可以使用

 mysql> desc my_table; 

如果你的列不能为null,那么当你将这个值设置为null时,它将是它的转换值。

这里是一个例子

 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) 

使用IS而不是=这将解决你的问题的例子语法:

 UPDATE studentdetails SET contactnumber = 9098979690 WHERE contactnumber IS NULL; 

对于那些面临类似问题的人,我发现当“模拟”一个SET = NULL查询时,PHPMyAdmin会抛出一个错误。 这是一个红鲱鱼..只是运行查询,一切都会好的。

空string的另一个可能的原因,而不是一个真正的 null是该字段是索引或是索引的一部分。 这发生在我身上:使用phpMyAdmin,我编辑了我的一个表中的字段结构,通过选中“ Null ”checkbox然后点击“ Save ”button来允许NULL 。 “ 表格定价已经被成功修改 ”,所以我认为这个改变发生了 – 事实并非如此。 在执行UPDATE将所有这些字段设置为NULL后 ,它们被设置为空string ,所以我再次查看了表结构,并看到该字段的“ Null ”列被设置为“ no ” 。 那时候我意识到这个字段是主键的一部分!

如果你遵循

 UPDATE table SET name = NULL 

那么名字是“”不是NULL在MYSQL中是指你的查询

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

我怀疑这里的问题是引号是作为string值中的文字input的。 您可以使用以下命令将这些列设置为null:

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

你当然应该首先检查这些值确实是“”,如:

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

用来代替=

例如: Select * from table_name where column is null

如果要使用更新查询设置列值为NULL(不带引号)更新表名set columnname = NULL

但是,如果您直接编辑mysql工作台中的字段值,则使用(Esc + del)keystroke将null值插入到选定列

首先, TABLE_NAME的列必须接受NULL ,并且您可能已经认识到为了使value NULL ,您需要使用

  Eg: UPDATE TABLE_NAME SET column_name = NULL Eg: instead of using column_name = 'NULL'. So, in a situation where one use a "selected value" to update a table in database, such as the following very simple example, Part one, Condition of the variable: Checkbox(name = "checkbox") Eg: if not click, $_POST["checkbox"] = "", Eg: if clicked, $_POST["checkbox"] = "1". Now, we must try to make this $_POST["checkbox"] to convert the value "" to NULL and update to the database table. An alternative way that I found is to use echo and assign echo value to a $variable of course, you already know how to use if {} else {}. To save echo value to a $variable, I did the following: Eg: ob_start() Eg: echo <==hint 1: selected value here. Eg: $variable1 = ob_get_contents() Eg: ob_end_clean() Then, you need to do this again, Eg: ob_start() Eg: echo <==hint 2: command line + selected value here. Eg: $variable2 = ob_get_contents() Eg: ob_end_clean() MYSQL will be something like the following: Eg: UPDATE table_name SET Eg: name = '$name', Eg: $variable2, Eg: date = '$date' Eg: WHERE id = '$id'; see the trick is to how to make Eg: $variable2 to show either Eg: checkbox = NULL or to show Eg: checkbox = '1' Hope this can give you a guide to solve this problem. I think you have enough knowledge to fill in the blanks. Good luck. Additional note: you can do the same for (MYSQL) INSERT INTO