我正在研究mySQL问题。我想设置一个翻译系统,其中键(占位符)保存在tblkeys中,翻译存储在tbltranslations中。
系统中的每个用户都有语言设置,当用户的语言翻译不可用时,我希望系统使用英语。
SELECT * FROM tblkeys, tbltranslations
WHERE
tblkeys.fiproject = 1
AND tbltranslations.fiproject = tblkeys.fiproject
AND tbltranslations.fikey = tblkeys.idkey
AND (tbltranslations.dtlanguage = 'de' OR tbltranslations.dtlanguage = 'en')
GROUP BY tblkeys.idkey
在这里,我使用简单的OR来证明我的意思。问题是,我希望系统更喜欢 de ,但如果不可用,请使用 en 作为dtlanguage。
我怎么能管理它?
提前感谢!
答案 0 :(得分:2)
您可以使用两个左连接。一个加入所有DE字符串,一个用于连接所有EN字符串,然后在您的选择中添加IFNULL(de.text,en.text)
selectr tblkeys.idkey, ifnull(de.translation, en.translation)
from tblkeys
left join
(select idkey, translation from tbltranslations where tbltranslations.dtlanguage = 'de') de
on de.idkey = tblkeys.idkey
left join
(select idkey, translation from tbltranslations where tbltranslations.dtlanguage = 'en') en
on en.idkey = tblkeys.idkey
答案 1 :(得分:0)
SELECT *,
IF( @key = tblkeys.idkey, @row := @row + 1, @row := 0 ),
@key := tblkeys.idkey
FROM tblkeys, tbltranslations
JOIN ( SELECT @row := 0, @key := 0 ) AS initialize
WHERE tblkeys.fiproject = 1 AND
tbltranslations.fiproject = tblkeys.fiproject AND
tbltranslations.fikey = tblkeys.idkey AND
tbltranslations.dtlanguage IN( 'de', 'en' ) AND
@row = 0
ORDER BY FIELD( tbltranslations.dtlanguage, 'de', 'en' )