使用多个AND,OR Mysql语句时我感到困惑。 例如,我有这个查询:
$ip_sql= $connector->query("select count(id) AS count FROM likes WHERE
content_id='$id' AND (ip='$ip' OR user='$_SESSION[user_id]')");
$rw90 = $connector->fetchArray($ip_sql);
$count = $rw90['count'];
if($count==0) {...}
在这里,我尝试检查用户是否喜欢内容。我想做一个双重检查:
我想我可以通过2个查询来完成,例如:
$result1 = $connector->query("SELECT count(id) as cn FROM likes
WHERE user='$current_user' AND content_id='$id'");
$row1 = $connector->fetchArray($result1);
if($row1['cn'] > 0) {/*stop here*/}
else {
$result2 = $connector->query("SELECT count(id) as cn2 FROM likes
WHERE ip='$current_ip' AND content_id='$id'");
$row2 = $connector->fetchArray($result2);
if($row2['cn2']>0) {/*stop here*/}
else {/*it's ok!*/}
}
但是我想在1中收集这2个查询。我的第一个代码不会返回我需要的内容。提前谢谢
刚试过以下代码。它也不起作用
select count(id) AS count
from likes where content_id='$id' and ((ip='$ip' OR user='$_SESSION[user_id]') OR (ip='$ip' AND user='$_SESSION[user_id]'))
答案 0 :(得分:1)
"select count(id) AS count FROM likes WHERE
content_id='" . $id . "' AND (ip='" .$ip . "' OR user='" . $_SESSION[user_id] . "')";
您的初始查询看起来是正确的 - >试试这个以确保正确传递变量。如果它不起作用,请告诉我,我会帮助您找到正确的答案!