我正在执行搜索功能,我有联系人组和语言,
这是mysql查询:
$sql = "SELECT * FROM contact where isdeleted = 0 ";
if ($language !="" && $language !="Empty" ){
$language_exp = explode(',', $language);
$sql .= " INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid
INNER JOIN language ON contactlanguage.languageid = language.id AND language.language in ('".implode("', '", $language_exp)."')";
}
if ($contactgroup !="" && $contactgroup !="Empty" ){
$contactgroup_exp = explode(',', $contactgroup);
$sql .= " AND contactgroup in ('".implode("', '", $contactgroup_exp)."')";
}
如果我只选择语言,那么首先会有三种情况,它会像这样执行查询
它不起作用
SELECT * FROM contact where isdeleted = 0 INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid INNER JOIN language ON contactlanguage.languageid = language.id AND language.language in ('English', '')
如果没有选择语言,则仅选择其工作的联系人组,
SELECT * FROM contact where isdeleted = 0 AND contactgroup in ('Contactgroup1', '')
第三种情况,如果我选择语言和联系人组,它不起作用
SELECT * FROM contact where isdeleted = 0 INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid INNER JOIN language ON contactlanguage.languageid = language.id AND language.language in ('English', '') AND contactgroup in ('Contactgroup1', '')
任何人都可以指导我如何执行案例1和案例3的查询,谢谢
答案 0 :(得分:0)
JOIN查询错误,您需要将JOIN语句之后的where条件移动为
SELECT * FROM contact
INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid
INNER JOIN language ON contactlanguage.languageid = language.id
where
isdeleted = 0
AND language.language in ('English', '')
答案 1 :(得分:0)
查询错误。您必须首先添加join
语句,然后添加where
SELECT * FROM contact INNER JOIN contactlanguage ON contact.id = ... where isdeleted = 0