我弄乱了我的where子句吗?获得意外结果

时间:2013-10-15 19:41:09

标签: php mysql where

$active_sth = $dbh->prepare("SELECT * FROM user_campaign 
    WHERE status='blasting' 
    OR status='ready' 
    OR status='followup_hold' 
    OR status='initial_hold' 
    AND uid=:uid 
    ORDER BY status ASC");

$active_sth->bindParam(':uid', $_SESSION['uid']['id']);

$active_sth->execute();

我是积极的$ _SESSION ['uid'] ['id'] = 7 但它也会提取id 10或任何其他数字的结果。

我的AND / OR子句写错了吗?

2 个答案:

答案 0 :(得分:4)

是的,查询错误

SELECT * FROM user_campaign 
WHERE (
    status='blasting' 
    OR status='ready' 
    OR status='followup_hold' 
    OR status='initial_hold'
)
AND uid=:uid 
ORDER BY status ASC

您必须对所有OR进行分组以确保该行获得其中一个值,并单独检查它是否已提供uid

答案 1 :(得分:3)

写这个的正确方法是:

SELECT * FROM user_campaign 
WHERE status IN ('blasting', 'ready', 'followup_hold', 'initial_hold')
AND uid =: uid 
ORDER BY status ASC

您应该使用IN而不是大量的OR s:)