Pierre S.
Pierre S.

Reputation: 21

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.

Upvotes: 1

Views: 961

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30809

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.

Upvotes: 3

Related Questions