简体   繁体   中英

SQL Query WHERE CLAUSE is not working as expected

I am using this query to get some price records from my database. If you look at the records you will find 1 unexpected row (4th row) in the result set according to the query. Am I running the query right or I have to change it to eliminate the unexpected row (row number 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;

The output:

       +------+---------+----+----+----+----+----+-------------+
       | 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)

That 4th row is not what I expected! It should not be in my result set:

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

EDIT For who wanted to see the 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
(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)

Your first condition is false BUT the second one is TRUE. Your OR will be true

So it is normal to see the 4th row in the result set.

Try changing the query depending on the result you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM