我有以下数据库结构
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
希望这可以帮助 :)