user2857795
user2857795

Reputation: 15

UPDATE with JOIN and GROUP_CONCAT

I have 4 tables, one of which I need to update.

Now I need to update the languages column in table t1 based on the information in tables t2,t3,t4.

I have written the following query, but it says SQL Error (1111): Invalid use of group function */

UPDATE t1 
LEFT JOIN t2
ON           t1.id = t2.id
LEFT JOIN t3 
ON           t2.id = t3.X_id
LEFT JOIN t4
ON           t3.languages_iso2 = t4.iso2
SET    t1.languages = GROUP_CONCAT(t4.`language` ORDER BY t4.language ASC)

I know that this solution can't be the most elegant one, but my SQL skills are not that good, so I don't know what else I should try. Does anyone have a solution for this problem? Thanks in advance!

Upvotes: 1

Views: 571

Answers (1)

Saharsh Shah
Saharsh Shah

Reputation: 29051

Try this:

UPDATE t1 
INNER JOIN (SELECT t2.id, GROUP_CONCAT(t4.language ORDER BY t4.language) languages 
            FROM t2
            INNER JOIN t3 ON t2.id = t3.X_id
            INNER JOIN t4 ON t3.languages_iso2 = t4.iso2
            GROUP BY t2.id
          ) AS t2 ON t1.id = t2.id
SET t1.languages = t2.languages;

Upvotes: 1

Related Questions