简体   繁体   中英

Update Group_Concat from multiple rows

I am trying to update 1 column with the results of concatenating several rows. I am able to do it in a Select query but can't figure it out in an Update query.

SELECT GROUP_CONCAT(SizeTemp SEPARATOR ', ') FROM Table GROUP BY ParentSKU

Also, the result separates everything with a coma which is fine but I need the last string NOT to be followed by a coma. In the example below: no coma after XL

S,M,L,XL

Thanks for any help.

You can use update with inner query as shown below:

UPDATE TABLE AS t1,
(SELECT ParentSKU, GROUP_CONCAT(SizeTemp SEPARATOR ', ') AS sizes FROM TABLE GROUP BY ParentSKU) AS t2
SET t1.sizes = t2.sizes
WHERE t1.ParentSKU = t2.ParentSKU
AND t1.ParentSKU = ?

Criteria/column may differ based on which column needs updating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM