Domanda

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

  • Table t1 needs to be updated according to the information in table t2 (t1.id = t2.id)

  • Table t2 contains information about websites (e.g.ID, traffic ).

  • Table t3 is a m:n table, that links the IDs in table t2 with the languages in table t4 based on language codes (ISO2) (e.g. XID: 1 | ISO2: EN,DE,FR)

  • Table t4 contains the ISO2-Codes (e.g. EN, DE, FR) and the respective languages (English, German, French)

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!

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top