我有一个多语言网站,并使用它来获取用户使用的语言:
$languagestring = array();
$res1 = "SELECT id, text FROM ".$prefix."_language WHERE lang = 'en' ORDER BY id ASC";
if($result1 = $mysqli->query($res1)){
while($row1 = $result1->fetch_object()){
$langid = $row1->id;
$res = "SELECT text FROM ".$prefix."_language WHERE fk_id = $langid AND lang = '$language'";
if($result = $mysqli->query($res)){
$row = $result->fetch_object();
if(!$row){
$languagestring[] = $row1->text;
} else {
$str = $row->text;
$languagestring[] = $str;
}
}
}
}
在这个脚本中,我首先获得默认的英语语言然后检查用户首选的语言是否有与英语相匹配的字符串。这就像一个魅力,但执行时间很长。是否可以将这两个查询合并为一个,如果是......怎么样?
提前致谢; - )
########编辑试着没有运气:
$res1 = "SELECT t1.text as etext, t2.text
FROM ".$prefix."_language t1 LEFT JOIN ".$prefix."_language t2 ON t2.fk_id = t1.id AND lang = 'da'";
if($result1 = $mysqli->query($res1)){
while($row1 = $result1->fetch_object()){
$row1->text.'<br>';
}
}
它不会产生任何结果吗?
答案 0 :(得分:0)
SELECT t1.text as etext, t2.text
FROM language t1 LEFT JOIN language t2 ON t2.fk_id=t1.id AND lang = '$language'
或类似的东西。
主要思想是将英语部分LEFT JOIN加入另一个语言部分。它将返回2个文本或仅返回英文
答案 1 :(得分:-1)
使用UNION和Subquery的解决方案可以正常工作,但考虑加载时间可能不是最佳选择。
"( select text from ".$prefix."_language where lang = '$langid')
union
( select text from ".$prefix."_language where lang = 'en' and fk_id not in ( select fk_id from ".$prefix."_language where lang = '$langid' ))";