Mysql
 sql >> Database >  >> RDS >> Mysql

mySQL Distinct Join?

Prova questo:

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

Demo SQLFiddle

MODIFICA :Potrebbe funzionare meglio:

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

Demo SQLFiddle