MySQL显示不正确的数据

时间:2012-10-05 18:52:08

标签: mysql

除了上面的问题,我想知道是否有任何方法可以优化这个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_notax等于0的产品,为什么?我说数字需要高于0 ...

任何帮助都将不胜感激。

5 个答案:

答案 0 :(得分: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),则显示它。

答案 1 :(得分:3)

多数民众赞成因为“和”优先于“或”而且在你的陈述中只有最后一部分将被“加”到category = 3246.你需要将括号()放在或部分周围。

答案 2 :(得分:1)

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

    WHERE category IN somearray

答案 3 :(得分:1)

当评估该WHERE子句时,任何ORed子句为true将使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;

答案 4 :(得分: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;