MySQL 查询将前缀连接到字段中的现有值

MySQL query to concat prefix to existing value in a field

我的table结构如下,

ID  Name    Source
1   John    first.jpg
2   Doe second.jpg
3   Mary    third.jpg
4   Kurian  four.jpg

我想通过在主机和主键前添加如下内容来更新 "Source"

http://example.com/1/first.jpg
http://example.com/2/second.jpg
http://example.com/3/third.jpg
http://example.com/4/four.jpg

尝试使用 CONCAT("http://example.com/"+id,Source) 但因截断不正确的 DOUBLE 值而失败:

如有任何建议,我们将不胜感激。

尝试

UPDATE table_name 
SET Source = CONCAT('http://example.com/', ID, '/', Source);

结果

| ID |   Name |          Source                 |
|----|--------|---------------------------------|
|  1 |   john |  http://example.com/1/first.jpg |
|  2 |    Doe | http://example.com/2/second.jpg |
|  3 |   Mary |  http://example.com/3/third.jpg |
|  4 | Kurian | http://example.com/4/fourth.jpg |

检查这个

 SELECT CONCAT("http://example.com/" , CONCAT(ID , CONCAT("/" , Source))) FROM table_name;

或者干脆

 SELECT CONCAT("http://example.com/" ,ID , "/" , Source) FROM table_name;

您可以在 sql 中进行迭代,但最简单的方法是创建一个 php 脚本,您可以在其中创建一个包含 table 行的数组 然后使用 dot si tax 将 ID 与域名和来源连接起来,例如:

$newval = ‘{$table[“ID”]}’ .“/Domainname.com/” . ’{$table[“Source”]}’;

其中$table是存储table

的关联数组的变量


然后创建一个新查询,在其中覆盖 table 数组

中每一行的源列

请尝试以下代码:

更新table_Name SET Source = concat(concat(CONCAT('http://example.com/',ID),'/'),source);