-1

除了上面的问题,我想知道是否有任何方法可以优化这个 MySQL 语句。

SELECT *
FROM `products`
WHERE
  `category` = 3073 OR
  `category` = 3074 OR
  `category` = 3100 OR
  `category` = 3102 OR
  `category` = 3106 OR
  `category` = 3109 OR
  `category` = 3111 OR
  `category` = 3115 OR
  `category` = 3130 OR
  `category` = 3134 OR
  `category` = 3144 OR
  `category` = 3146 OR
  `category` = 3152 OR
  `category` = 3157 OR
  `category` = 3162 OR
  `category` = 3163 OR
  `category` = 3164 OR
  `category` = 3166 OR
  `category` = 3167 OR
  `category` = 3168 OR
  `category` = 3170 OR
  `category` = 3171 OR
  `category` = 3177 OR
  `category` = 3181 OR
  `category` = 3182 OR
  `category` = 3184 OR
  `category` = 3190 OR
  `category` = 3191 OR
  `category` = 3192 OR
  `category` = 3213 OR
  `category` = 3224 OR
  `category` = 3227 OR
  `category` = 3228 OR
  `category` = 3235 OR
  `category` = 3238 OR
  `category` = 3239 OR
  `category` = 3240 OR
  `category` = 3244 OR
  `category` = 3245 OR
  `category` = 3246 AND
  `active` = 1 AND
  `price_notax` > 0 AND
  `deleted` = 0
ORDER BY `position` ASC
LIMIT 0, 24;

该语句还显示字段值为price_notaxequal的产品0,为什么?我说这个数字需要高于0...

任何帮助,将不胜感激。

4

5 回答 5

5
SELECT *
FROM products
WHERE category in (3073, 3074) -- and so on
AND `active` = 1
AND `price_notax` > 0 
AND `deleted` = 0 
ORDER BY `position` -- ASC is by default
LIMIT 0, 24

您的查询显示错误的结果,因为您使用了 OR AND,因此如果只有一个条件为真(例如 category=3100),那么它会显示它。

于 2012-10-05T18:55:48.143 回答
3

那是因为“and”优先于“or”,并且在您的语句中,只有最后一部分将“anded”到category= 3246。您需要在 or 部分周围放置方括号 ()。

于 2012-10-05T18:55:12.670 回答
1

为什么不创建这些类别 ID 的数组,然后使用

    WHERE category IN somearray
于 2012-10-05T18:56:20.463 回答
1

当评估该 WHERE 子句时,任何 ORed 子句为真将使 AND 子句的状态无关紧要。要纠正这个问题,您必须用括号括住一组 OR 子句,以便单独评估它们......

但是要以更高的可读性完成同样的事情,请改用 IN 子句:

SELECT * 
FROM products 
WHERE category IN (3073, 3074, 3100, 3102, 3106, 3109, 3111, 3115, 3130, 3134, 3144, 3146, 3152, 3157, 3162, 3163, 3164, 3166, 3167, 3168, 3170, 3171, 3177, 3181, 3182, 3184, 3190, 3191, 3192, 3213, 3224, 3227, 3228, 3235, 3238, 3239, 3240, 3244, 3245, 3246) 
AND active = 1 
AND price_notax > 0 
AND deleted = 0 
ORDER BY position ASC LIMIT 0, 24;
于 2012-10-05T18:57:00.043 回答
0

OR的优先级低于AND

更正确的 SQL:

SELECT * 
FROM `products` 
WHERE ( 
    `category` = 3073 OR `category` = 3074 OR `category` = 3100 OR 
    `category` = 3102 OR `category` = 3106 OR `category` = 3109 OR 
    `category` = 3111 OR `category` = 3115 OR `category` = 3130 OR 
    `category` = 3134 OR `category` = 3144 OR `category` = 3146 OR 
    `category` = 3152 OR `category` = 3157 OR `category` = 3162 OR 
    `category` = 3163 OR `category` = 3164 OR `category` = 3166 OR 
    `category` = 3167 OR `category` = 3168 OR `category` = 3170 OR 
    `category` = 3171 OR `category` = 3177 OR `category` = 3181 OR 
    `category` = 3182 OR `category` = 3184 OR `category` = 3190 OR 
    `category` = 3191 OR `category` = 3192 OR `category` = 3213 OR 
    `category` = 3224 OR `category` = 3227 OR `category` = 3228 OR 
    `category` = 3235 OR `category` = 3238 OR `category` = 3239 OR 
    `category` = 3240 OR `category` = 3244 OR `category` = 3245 OR 
    `category` = 3246 )
    AND `active` = 1 
    AND `price_notax` > 0 
    AND `deleted` = 0 
ORDER BY `position` ASC 
LIMIT 0, 24;
于 2012-10-05T18:59:53.163 回答