Cómo actualizar columna con valor nulo


Estoy usando mysql y necesito actualizar una columna con un valor null. He intentado esto de muchas maneras diferentes y lo mejor que he conseguido es una cadena vacía.

¿Hay una sintaxis especial para hacer esto?

Author: linuxbuild, 2010-10-06

11 answers

Sin sintaxis especial:

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)
 174
Author: Daniel Vassallo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-06 08:42:05

NULL es un valor especial en SQL. Así que para anular una propiedad, haga esto:

UPDATE table SET column = NULL;
 31
Author: Gumbo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-06 08:11:33

Use IS en lugar de = Esto resolverá tu problema sintaxis de ejemplo:

UPDATE studentdetails
SET contactnumber = 9098979690
WHERE contactnumber IS NULL;
 7
Author: Krishna Chaitu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 05:44:01

Recuerde buscar si su columna puede ser null. Puedes hacerlo usando

mysql> desc my_table;

Si tu columna no puede ser null, cuando establezcas el valor en null, será el valor de cast.

Aquí un ejemplo

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)
 6
Author: Thiago Mata,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-08 21:01:53

Para aquellos que enfrentan un problema similar, descubrí que al 'simular' una consulta SET = NULL, phpMyAdmin lanzaría un error. Es una pista falsa.. simplemente ejecute la consulta y todo estará bien.

 4
Author: Daniel Dunn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-19 16:47:59

Otra posible razón para la cadena vacía, en lugar de un verdadero nulo es que el campo es un índice o es parte de un índice. Esto me sucedió a mí: usando phpMyAdmin, edité la estructura de un campo en una de mis tablas para permitir NULL marcando la casilla "Null" y luego presionando el botón "Save". "El precio de la tabla se ha alterado con éxito " se mostró, así que asumí que el cambio ocurrió't no lo hizo. UPDATEpara establecer todos esos campos en NULL, en su lugar, se establecieron en cadenas vacías, así que eché un vistazo a la estructura de la tabla de nuevo y vi que la columna " Null" para ese campo se estableció en ' no'. Fue entonces cuando me di cuenta de que el campo era parte de la clave primaria !

 1
Author: ReverseEMF,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-06 00:52:27

Si desea establecer un valor null utilizando la consulta de actualización, establezca el valor de columna en NULL (sin comillas) update tablename set columnname = NULL

Sin embargo, si está editando directamente el valor del campo dentro de mysql workbench, use la tecla (Esc + del) para insertar el valor nulo en la columna seleccionada

 1
Author: mayank nigam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-16 09:43:21

Si sigues

UPDATE table SET name = NULL

Entonces el nombre es "" no NULO EN MYSQL significa su consulta

SELECT * FROM table WHERE name = NULL no trabajar o decepcionar a sí mismo

 0
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-06 08:27:35

Sospecho que el problema aquí es que las comillas se ingresaron como literales en el valor de la cadena. Puede establecer estas columnas en null usando:

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

Por supuesto, primero debe comprobar que estos valores son de hecho "" con algo como:

SELECT DISTINCT(col) FROM table WHERE length(col)<3;
 0
Author: Dylan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-16 08:29:15

Use is en lugar de =

Eg: Select * from table_name where column is null

 0
Author: Cyber Gangster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-24 09:40:31

En las respuestas anteriores, se han sugerido muchas formas y repeticiones para lo mismo. Seguí buscando una respuesta como se mencionó es la pregunta, pero no pude encontrar aquí.

Pero frente a la pregunta anterior " actualizar una columna con un valor nulo" Podría ser "ACTUALIZAR TODAS LAS FILAS DE LA COLUMNA A NULL"

En tal situación siguiendo obras

> update table_name

> set field_name = NULL

> where field_name is not NULL;

is también is not funciona en mysql

 0
Author: Nitish Kumar Pal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-24 06:39:23