Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

Ein DISTINCT Join in MySQL

Versuchen Sie dies:

SELECT a.locale, c.text AS text_1, d.text AS text_2 FROM (SELECT DISTINCT locale FROM i18n) a CROSS JOIN table_1 b LEFT JOIN i18n c ON b.text_1 = c.id_i18n AND a.locale = c.locale LEFT JOIN i18n d ON b.text_2 = d.id_i18n AND a.locale = d.locale

SQLFiddle Demo

BEARBEITUNG: Das könnte besser funktionieren:

SELECT a.locale, b.text_1, c.text_2 FROM (SELECT DISTINCT locale FROM i18n) a LEFT JOIN ( SELECT b.locale, b.text AS text_1 FROM table_1 a JOIN i18n b ON a.text_1 = b.id_i18n ) b ON a.locale = b.locale LEFT JOIN ( SELECT b.locale, b.text AS text_2 FROM table_1 a JOIN i18n b ON a.text_2 = b.id_i18n ) c ON a.locale = c.locale

SQLFiddle Demo

Die Kernidee ist, von der distinkten Menge der Locales auszugehen und dann jede übersetzte Textspalte separat zu joinen, sodass jede Locale ihre eigenen text_1- und text_2-Werte erhält.