SQL ACTUALIZAR todos los valores en un campo con cadena anexa CONCAT no funciona


Esto es lo que quiero hacer:

Tabla actual:

+----+-------------+  
| id | data        |  
+----+-------------+  
|  1 | max         |  
|  2 | linda       |  
|  3 | sam         |  
|  4 | henry       |  
+----+-------------+  

Consulta misteriosa (algo así como "UPDATE table SET data = CONCAT(data, 'a')")

Tabla resultante:

+----+-------------+  
| id | data        |  
+----+-------------+  
|  1 | maxa        |  
|  2 | lindaa      |  
|  3 | sama        |  
|  4 | henrya      |  
+----+-------------+  

Eso es todo! Solo necesito hacerlo en una sola consulta, pero parece que no puedo encontrar una manera. Estoy usando MySQL en bluehost (creo que su versión 4.1)

Gracias a todos.

Author: Hugo Pakula, 2010-11-09

7 answers

Eso es casi todo lo que necesitas:

mysql> select * from t;
+------+-------+
| id   | data  |
+------+-------+
|    1 | max   |
|    2 | linda |
|    3 | sam   |
|    4 | henry |
+------+-------+
4 rows in set (0.02 sec)

mysql> update t set data=concat(data, 'a');
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from t;
+------+--------+
| id   | data   |
+------+--------+
|    1 | maxa   |
|    2 | lindaa |
|    3 | sama   |
|    4 | henrya |
+------+--------+
4 rows in set (0.00 sec)

No estoy seguro de por qué tendrías problemas, aunque estoy probando esto en 5.1.41

 206
Author: Marc B,
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-11-08 21:46:14

CONCAT con un valor null devuelve null, por lo que la solución más fácil es:

UPDATE MyTable SET spares = IFNULL( CONCAT (spares , "string" ), "string")

 28
Author: andrejc,
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-11-14 11:38:10
UPDATE mytable SET spares = CONCAT(spares, ',', '818') WHERE id = 1

No funciona para mí.

Repuestos es NULL por defecto pero es varchar

 9
Author: DS_web_developer,
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
2012-10-29 08:41:57

Resuelto. Resulta que la columna tenía un conjunto limitado de caracteres que aceptaría, lo cambió, y ahora la consulta funciona bien.

 6
Author: Fresheyeball,
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-11-09 03:20:09

Convierta los valores NULL con una cadena vacía envolviéndola en COALESCE

"UPDATE table SET data = CONCAT(COALESCE(`data`,''), 'a')"

O

Use CONCAT_WS en su lugar:

"UPDATE table SET data = CONCAT_WS(',',data, 'a')"
 5
Author: Rohan Khude,
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
2015-11-04 10:02:13
UPDATE 
    myTable
SET 
    col = CONCAT( col , "string" )

No pudo solucionarlo. La sintaxis de la solicitud era correcta, pero" línea 0 afectada " cuando se ejecutaba.

La solución fue:

UPDATE 
    myTable 
SET 
    col = CONCAT( myTable.col , "string" )

Ese funcionó.

 3
Author: Jeremy Thille,
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
2012-09-21 16:49:49

Puedes hacer esto:

Update myTable
SET spares = (SELECT CASE WHEN spares IS NULL THEN '' ELSE spares END AS spares WHERE id = 1) + 'some text'
WHERE id = 1

Field = field + value no funciona cuando field es null.

 1
Author: Eric,
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-05-24 18:19:23