Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL查询WHERE子句未按预期工作_Mysql_Sql - Fatal编程技术网

Mysql SQL查询WHERE子句未按预期工作

Mysql SQL查询WHERE子句未按预期工作,mysql,sql,Mysql,Sql,我使用此查询从数据库中获取一些价格记录。如果查看这些记录,您将根据查询在结果集中发现1个意外的行(第4行)。我是正确运行查询,还是必须对其进行更改以消除意外的行(第4行) 输出: +------+---------+----+----+----+----+----+-------------+ | shop | price | p3 | ds | cp | ol | af | colors | +------+---------+----+

我使用此查询从数据库中获取一些价格记录。如果查看这些记录,您将根据查询在结果集中发现1个意外的行(第4行)。我是正确运行查询,还是必须对其进行更改以消除意外的行(第4行)

输出:

       +------+---------+----+----+----+----+----+-------------+
       | 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)
第四排不是我所期望的!它不应该在我的结果集中:

       |   38 | 8490.00 |  1 |  0 |  0 |  0 |  0 | NULL        |
为希望查看创建表的用户编辑:

     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.ds=0和m.优惠券活跃=0和m.affiliate=0和m.online=0)似乎是正确的,与或相关,这将在结果中存储颜色,因为逗号分隔列表不是最好的主意。虽然在没有样本数据的情况下很难给出准确的答案,但请尝试更改
和(m.p3=1和m.colors不为空)或(m.ds=0和m.优惠券活跃=0和m.affiliate=0和m.online=0)
to
和((m.p3=1和m.colors不为空)或(m.ds=0和m.优惠券活跃=0和m.affiliate=0和m.online=0))
此外,您的GROUP BY子句在此查询中毫无意义且具有误导性。当
左连接
时,您通常在
ON
子句中使用右侧表条件来实现
左连接
行为。(在
WHERE
子句中,您会得到常规的
内部连接
结果。)我将用您需要的查询编辑我的答案,只需告诉我们您在所有条件下到底想要什么(我真的不知道是否将OR更改为和是您所期望的,我想不是)事实上,我尝试了很多方法,但都未能消除第四行。这就是为什么我在这里寻求你们的帮助。@Theo请注意,这里的两个组条件也包含在括号中,有人通过编辑问题消除了这两个组条件。
     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)