0

我有一个看似相对简单的 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,但不会同时返回两者。我做错了什么?

4

2 回答 2

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;"; 
于 2010-11-09T15:45:00.530 回答
0

看起来你缺少括号,不是吗?

A or B and C or D有歧义,你需要用括号来明确你的意思是(A or B)和(C or D)

于 2010-11-09T15:48:47.493 回答