1

我有以下查询:

$query="SELECT * FROM products 
                 WHERE  productname LIKE '%$searchterm%' 
                 OR shortdescription LIKE '%$searchterm%' 
                 OR description LIKE  '%$searchterm%' AND VISIBLE != 0";

该查询返回我想要的记录,但也返回可见 = 0 的记录。

谁能告诉我我做错了什么?

问候,佐兰

4

2 回答 2

1

使用括号:

SELECT 
    * 
FROM 
    products 
WHERE  
    (
        productname LIKE '%$searchterm%' 
     OR shortdescription LIKE '%$searchterm%' 
     OR description LIKE  '%$searchterm%'
    ) 
AND VISIBLE != 0
于 2013-04-10T00:38:11.327 回答
1

这是关于运算符优先级的

运算符AND先运行,然后OR

所以你的查询是这样的:

$query="SELECT * FROM products 
                 WHERE  productname LIKE '%$searchterm%' 
                 OR shortdescription LIKE '%$searchterm%' 
                 OR (description LIKE  '%$searchterm%' AND VISIBLE != 0");

将您的查询修复为:

$query="SELECT * FROM products 
                 WHERE  (productname LIKE '%$searchterm%' 
                 OR shortdescription LIKE '%$searchterm%' 
                 OR description LIKE  '%$searchterm%') AND VISIBLE != 0";
于 2013-04-10T00:50:38.357 回答