SQL查询WHERE CLAUSE未按预期工作

时间:2015-12-04 07:41:50

标签: mysql sql

我正在使用此查询从我的数据库中获取一些价格记录。如果查看记录,您将根据查询在结果集中找到1个意外行(第4行)。我是否正确运行查询,或者我必须更改它以消除意外行(第4行)?

     mysql> SELECT m.shop,
       m.price,
       m.p3,
       m.ds,
       m.coupon_active AS cp,
       m.online AS ol,
       m.affiliate AS af,
       m.colors
FROM shop_product_m2m m
LEFT JOIN shop s ON s.id=m.shop
WHERE product = 1971
  AND s.is_active = 1
  AND m.price IS NOT NULL
  AND m.price != ''
  AND (
       (m.p3=1 AND m.colors IS NOT NULL)
       OR 
       (m.ds =0 AND m.coupon_active=0 AND m.affiliate=0 AND m.online=0)
      )
ORDER BY m.p3 DESC,
         m.price ASC,
         m.modified DESC;

输出:

       +------+---------+----+----+----+----+----+-------------+
       | shop | price   | p3 | ds | cp | ol | af | colors      |
       +------+---------+----+----+----+----+----+-------------+
       |  317 | 8350.00 |  1 |  0 |  0 |  0 |  0 | black       |
       |  268 | 8490.00 |  1 |  0 |  0 |  0 |  0 | Black,White |
       |  192 | 8490.00 |  1 |  0 |  0 |  0 |  0 | White,Black |
       |   38 | 8490.00 |  1 |  0 |  0 |  0 |  0 | NULL        |
       |  166 | 8110.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  160 | 8250.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  184 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  182 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  181 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  112 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |   90 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  130 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |   68 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |   66 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  150 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |   91 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  124 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  151 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       |  159 | 8490.00 |  0 |  0 |  0 |  0 |  0 | NULL        |
       +------+---------+----+----+----+----+----+-------------+                                                                  
       19 rows in set (0.00 sec)

第4排不是我的预期!它不应该在我的结果集中:

       |   38 | 8490.00 |  1 |  0 |  0 |  0 |  0 | NULL        |

编辑对于谁想要查看CREATE TABLE:

     CREATE TABLE `shop_product_m2m` (
         `id` int(11) NOT NULL AUTO_INCREMENT,
         `created` datetime NOT NULL,
         `modified` datetime NOT NULL,
         `expires` datetime NOT NULL,
         `shop` int(11) NOT NULL,
         `product` int(11) NOT NULL,
         `price` decimal(13,2) DEFAULT NULL,
         `instock` tinyint(1) NOT NULL DEFAULT '1',
         `expired` tinyint(1) NOT NULL DEFAULT '0',
         `burgainable` tinyint(1) NOT NULL DEFAULT '1',
         `coupon_active` tinyint(1) NOT NULL DEFAULT '0',
         `referral_url` text,
         `p3` tinyint(1) NOT NULL DEFAULT '0',
         `warrantytype` varchar(10) NOT NULL DEFAULT 'None',
         `warranty` text,
         `colors` varchar(128) DEFAULT NULL,
         `ds` tinyint(1) NOT NULL DEFAULT '0',
         `affiliate` tinyint(1) NOT NULL DEFAULT '0',
         `online` tinyint(1) NOT NULL DEFAULT '0',
         `http_status` varchar(3) DEFAULT NULL,
         `color_varies` tinyint(1) NOT NULL DEFAULT '0',
         `price_variance_count` int(11) DEFAULT NULL,
          PRIMARY KEY (`id`),
          UNIQUE KEY `shop_2` (`shop`,`product`),
          KEY `shop` (`shop`),
          KEY `product` (`product`),
          CONSTRAINT `shop_product_m2m_ibfk_1` FOREIGN KEY (`shop`) 
          REFERENCES `shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
          CONSTRAINT `shop_product_m2m_ibfk_2` FOREIGN KEY (`product`) 
          REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB AUTO_INCREMENT=77002 DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:1)

(m.p3=1 AND m.colors IS NOT NULL) 
    OR 
(m.ds =0 and m.coupon_active=0 and m.affiliate=0 and m.online=0)

你的第一个条件是假但是第二个条件是真的。 您的 OR 将是真的

因此,查看结果集中的第4行是正常的。

根据您需要的结果尝试更改查询。