许多BETWEEN和AND错误回答mysql

时间:2013-02-01 00:04:00

标签: php mysql

我使用查询有点复杂。 BETWEEN和。我所掌握的是具有数据库所有值的查询。字段classe ='Vehicule'不再重新调整,并选择了孔表元素 这里是简单的代码

$query = "SELECT
publier.classe AS 'classe',
publier.sclasse AS 'sclasse',
publier.region AS 'region',
publier.sujet AS 'sujet',
publier.prix AS 'prix',
publier.details AS 'details',
publier.pdate AS 'pdate',
publier.ptime AS 'ptime',
publier.photo1 AS 'photo1',
publier.photo2 AS 'photo2',
publier.photo3 AS 'photo3',
publier.photo3 AS 'photo4',
publier.courriel AS 'courriel',
info.Ville AS 'Ville' ,
info.Nom AS 'Nom',
info.Prenom AS 'Prenom',
info.Telephone AS 'Telephone',
info.Courriel AS 'Courriel'
FROM 
publier LEFT JOIN info 
ON
publier.courriel = info.Courriel 
        WHERE classe = 'Vehicule' AND  
        (details LIKE \"%$trimmed%\") OR 
        (sujet  LIKE \"%$trimmed%\") AND 
        (pdate BETWEEN DATE_SUB(NOW(), INTERVAL 60 DAY) AND NOW()) AND 
        (prix BETWEEN '".$pmin."' AND '".$pmax."') 
ORDER BY prix ASC";

2 个答案:

答案 0 :(得分:2)

小心WHERE中的条件如何分组:

WHERE classe = 'Vehicule' AND  (details like \"%$trimmed%\")OR(sujet  like \"%$trimmed%\")  AND (pdate BETWEEN DATE_SUB(NOW(), INTERVAL 60 DAY) AND NOW()) AND (prix BETWEEN '".$pmin."' AND '".$pmax."')

这句话

WHERE A AND B OR C

不同
WHERE A AND (B OR C)

答案 1 :(得分:2)

变化

 (details like \"%$trimmed%\")OR(sujet  like \"%$trimmed%\")

 (details like \"%$trimmed%\" OR sujet like \"%$trimmed%\")

OR 是罪魁祸首,当外括号时,所有先前条件可能都为假,结果仍然会被返回。