SQL更新字段中的所有值,附加字符串CONCAT无效

时间:2022-12-25 07:56:59

Here is what I want to do:

下面是我想做的:

current table:

当前表:

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

Mystery Query ( something like "UPDATE table SET data = CONCAT(data, 'a')" )

神秘查询(类似“UPDATE table SET data = CONCAT(data, 'a')”))

resulting table:

结果表:

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

thats it! I just need to do it in a single query, but can't seem to find a way. I am using mySQL on bluehost (I think its version 4.1)

这就是它!我只需要在一个查询中完成,但似乎找不到方法。我在bluehost上使用mySQL(我认为是4.1版本)

Thanks everyone.

谢谢每一个人。

7 个解决方案

#1


191  

That's pretty much all you need:

这就是你所需要的:

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)

Not sure why you'd be having trouble, though I am testing this on 5.1.41

不知道为什么你会有麻烦,尽管我在5.1.41测试这个。

#2


28  

CONCAT with a null value returns null, so the easiest solution is:

具有null值的CONCAT返回null,所以最简单的解决方案是:

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

更新myTable SET spares = IFNULL (CONCAT(spares, "string"), "string")

#3


9  

UPDATE mytable SET spares = CONCAT(spares, ',', '818') WHERE id = 1

not working for me.

不为我工作。

spares is NULL by default but its varchar

备件默认为空,但它的varchar

#4


6  

Solved it. Turns out the column had a limited set of characters it would accept, changed it, and now the query works fine.

解决它。结果显示,该列有一组有限的字符,它将接受、修改它,现在查询运行良好。

#5


5  

convert the NULL values with empty string by wrapping it in COALESCE

通过将空字符串封装在合并中,将NULL值转换为空字符串。

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

OR

Use CONCAT_WS instead:

用CONCAT_WS代替:

"UPDATE table SET data = CONCAT_WS(',',data, 'a')"

#6


2  

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

Could not work it out. The request syntax was correct, but "0 line affected" when executed.

无法解决。请求语法是正确的,但是执行时“0行受影响”。

The solution was :

解决方案是:

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

That one worked.

这一个工作。

#7


1  

You can do this:

你可以这样做:

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 does not work when field is null.

字段=字段+值在字段为空时无效。

#1


191  

That's pretty much all you need:

这就是你所需要的:

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)

Not sure why you'd be having trouble, though I am testing this on 5.1.41

不知道为什么你会有麻烦,尽管我在5.1.41测试这个。

#2


28  

CONCAT with a null value returns null, so the easiest solution is:

具有null值的CONCAT返回null,所以最简单的解决方案是:

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

更新myTable SET spares = IFNULL (CONCAT(spares, "string"), "string")

#3


9  

UPDATE mytable SET spares = CONCAT(spares, ',', '818') WHERE id = 1

not working for me.

不为我工作。

spares is NULL by default but its varchar

备件默认为空,但它的varchar

#4


6  

Solved it. Turns out the column had a limited set of characters it would accept, changed it, and now the query works fine.

解决它。结果显示,该列有一组有限的字符,它将接受、修改它,现在查询运行良好。

#5


5  

convert the NULL values with empty string by wrapping it in COALESCE

通过将空字符串封装在合并中,将NULL值转换为空字符串。

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

OR

Use CONCAT_WS instead:

用CONCAT_WS代替:

"UPDATE table SET data = CONCAT_WS(',',data, 'a')"

#6


2  

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

Could not work it out. The request syntax was correct, but "0 line affected" when executed.

无法解决。请求语法是正确的,但是执行时“0行受影响”。

The solution was :

解决方案是:

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

That one worked.

这一个工作。

#7


1  

You can do this:

你可以这样做:

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 does not work when field is null.

字段=字段+值在字段为空时无效。