Update mysql field using CONCAT and SELECT

孤者浪人 提交于 2019-12-03 21:37:35
Kickstart

Do you need the sub query?

UPDATE tbl 
SET string_id = CONCAT(string_id, ',13,14,15,16')
WHERE id = @id;

Note that in MySQL you cannot modify using an UPDATE the table that is used in the sub query (although there are fiddles around it):-

https://dev.mysql.com/doc/refman/5.5/en/subqueries.html

In MySQL, you cannot modify a table and select from the same table in a subquery. This applies to statements such as DELETE, INSERT, REPLACE, UPDATE, and (because subqueries can be used in the SET clause) LOAD DATA INFILE.

Nailgun

Try to use UPDATE without INTO:

set @id = 3;

UPDATE tbl 
SET string_id = 
    CONCAT((
        SELECT tbl_alias.string_id 
        FROM tbl as tbl_alias
        WHERE id = @id
    ),',13,14,15,16') WHERE id = @id;

Update:

Try this:

set @id = 3;

UPDATE tbl 
SET string_id = 
    CONCAT(SELECT string_id FROM (
        SELECT tbl_alias.string_id 
        FROM tbl as tbl_alias
        WHERE id = @id
    ) t1 ,',13,14,15,16') WHERE id = @id;

This case helps sometimes to update ALL column data for TESTING purpose:

UPDATE customer_profile 
  SET businessId = CONCAT(businessId, ' new customer ', (id + 1))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!