4

次のデータベース構造があります

table_1
  text_1 (INT)
  text_2 (INT)


i18n
  id_i18n (PK INT)
  locale  (PK VARCHAR(5))
  text    (TEXT)

table_1 の列 text_1 と text_2 は、いくつかの i18n.id_i18n エントリを指す外部キーです。特定のロケールのエントリに簡単に参加できます

SELECT t1.text as text_1, t2.text as text_2
FROM table_1
LEFT JOIN i18n as t1 ON text_1 = t1.id_i18n and t1.locale = "en_us"
LEFT JOIN i18n as t2 ON text_2 = t2.id_i18n and t2.locale = "en_us"

次のものも取得できます

row1: locale, text_1, text_2
row2: locale, text_1, text_2
row3: locale, text_1, null
row4: locale, text_1, text_2

このクエリを使用して

SELECT t1.text as text_1, t2.text as text_2
FROM room
LEFT JOIN i18n as t1 ON text_1 = t1.id_i18n
LEFT JOIN i18n as t2 ON text_2 = t2.id_i18n and t1.locale = t2.locale
group by t1.locale;

次のi18nエントリがあると仮定します

      id_i18n   locale  text
row1: 1         en_us   text_1_for[en_us]
row2: 1         en_gb   text_1_for[en_gb]
row3: 1         el_gr   text_1_for[el_gr]
row4: 2         en_us   text_2_for[en_us]
row5: 2         en_gb   text_2_for[en_gb]
row6: 2         pr_pk   text_2_for[pr_pk]
row7: 1         en_ca   text_1_for[en_ca]

次に、tbl という名前のテーブルにリンクするテーブル

     id, i18n_text_1, i18n_text_2
row1 1   1            2

次のような結果セットを生成したい

      locale,  text_1,              text_2
row1: en_us    text_1_for[en_us]    text_2_for[en_us]
row2: en_gb    text_1_for[en_gb]    text_2_for[en_gb]
row3: el_gr    text_1_for[el_gr]    null
row4: pr_pk    null                 text_2_for[pr_pk]
row5: en_ca    text_1_for[en_ca]    null

お役に立てれば :)

4

2 に答える 2

3

これを試して:

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 デモ


編集:これはうまくいくかもしれません:

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 デモ

于 2012-07-28T20:24:20.093 に答える
0

できますか

SELECT text
  FROM table_1
  JOIN i18n ON i18n_title = i18n.id_i18n OR i18n_name = i18n.id_i18n
于 2012-07-28T19:34:47.597 に答える