fI似乎是一个相对简单的AND / OR条款让我有点头疼。
$query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
$query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
$query .= " LEFT JOIN $tb_content tb_content ON stc.contentid=tb_content.id ";
$query .= " WHERE stv.name='".$region."' AND stc.contentid IN (".implode($cIDs,",").") ";
$query .= " OR stv.name='".$countries."' AND stc.contentid IN (".implode($cIDs,",").") ";
$query .= " AND tb_content.pub_date >= '$pub_date' ";
$query .= " AND tb_content.published = 1 ";
$query .= " ORDER BY stc.contentid ASC;";
我需要它来检索$region
和$countries
的值。但是,它的方式只返回$countries
的值。如果我使用OR
删除该行,则会返回$region
的值,但不会返回两者。我做错了什么?
答案 0 :(得分:2)
尝试在OR
两侧的子句周围加上括号。见下文:
$query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
$query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
$query .= " LEFT JOIN $tb_content tb_content ON stc.contentid=tb_content.id ";
$query .= " WHERE (stv.name='".$region."' AND stc.contentid IN (".implode($cIDs,",").")) ";
$query .= " OR (stv.name='".$countries."' AND stc.contentid IN (".implode($cIDs,",").")) ";
$query .= " AND tb_content.pub_date >= '$pub_date' ";
$query .= " AND tb_content.published = 1 ";
$query .= " ORDER BY stc.contentid ASC;";
答案 1 :(得分:0)
看起来你缺少括号,不是吗?
A或B和C或D 不明确,您需要使用括号表明您的意思(A或B)和(C或D) < / p>