我已经在5.5.35和5.6.19上试过了。两者都返回不同的结果。 我基本上是想找到一家公司达到某个总分值(在本例中为 250)以上的确切日期
如果我的最终 sql 查询没有错误,5.5.35 似乎返回了预期的结果。
DROP TABLE IF EXISTS company_sales;
CREATE TABLE `company_sales` (
`id_company_sales` bigint(20) NOT NULL AUTO_INCREMENT,
`company_id` int(11) NOT NULL,
`order_id` int(11) DEFAULT NULL,
`date` date NOT NULL,
`points` decimal(10,2) unsigned NOT NULL,
`cancelled` tinyint(1) DEFAULT '0',
`cnd1` int(11) DEFAULT NULL,
`cnd2` varchar(128) DEFAULT '',
`cnd3` mediumint(8) NOT NULL DEFAULT '0',
PRIMARY KEY (`id_company_sales`),
UNIQUE KEY `cnd1_UNIQUE` (`cnd1`),
KEY `index3` (`company_id`,`date`),
KEY `order_id` (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=56919 DEFAULT CHARSET=latin1;
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (48712,6978,64390,'0000-00-00',76.96,1,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (49093,6978,0,'2014-11-01',256.03,0,NULL,'starting',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (47286,6978,63122,'2014-11-23',84.44,1,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (48956,6978,64811,'2014-11-28',76.96,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (48962,6978,64817,'2014-11-28',84.44,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (49042,6978,64900,'2014-11-28',57.52,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (49048,6978,64905,'2014-11-28',28.76,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (49090,6978,64945,'2014-11-28',8.35,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (53416,6978,0,'2014-12-01',106.80,0,NULL,'starting',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (53415,6978,69189,'2014-12-09',26.10,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (53459,6978,69231,'2014-12-09',16.55,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (53959,6978,69701,'2014-12-10',64.15,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (57880,4605,73805,'2014-12-18',92.13,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (57879,6212,73804,'2014-12-18',21.12,0,NULL,'',0);
INSERT INTO `company_sales` (`id_company_sales`,`company_id`,`order_id`,`date`,`points`,`cancelled`,`cnd1`,`cnd2`,`cnd3`) VALUES (57878,7395,73626,'0000-00-00',39.97,1,NULL,'',0);
SELECT
NULL AS id_company_sales,
NULL AS date,
NULL AS points,
NULL AS total
FROM DUAL WHERE
(@total:=0)
UNION SELECT
id_company_sales,
(date),
points,
@total:=@total + points AS total
FROM
company_sales cs
WHERE
cs.company_id = 6978
AND cs.date >= '2014-11-21'
AND cs.date <= '2014-12-21'
AND cs.cancelled = 0
AND cs.order_id != 0
AND @total < 250
ORDER BY id_company_sales DESC
LIMIT 1;
编辑:我已经提交了一份关于 MySQL 的错误报告 http://bugs.mysql.com/bug.php?id=75266
最佳答案
像这样使用用户定义的变量(例如 @total
)有点不确定。如果您在相同的 MySQL 版本上运行相同的查询,它可能会起作用。但是,在 MySQL 版本更新时,查询优化器可能已更改并在内部以不同方式执行相同的查询。
MySQL manual还指出:
As a general rule, other than in SET statements, you should never assign a value to a user variable and read the value within the same statement. For example, to increment a variable, this is okay:
SET @a = @a + 1;
For other statements, such as SELECT, you might get the results you expect, but this is not guaranteed. In the following statement, you might think that MySQL will evaluate @a first and then do an assignment second:
SELECT @a, @a:=@a+1, ...;
However, the order of evaluation for expressions involving user variables is undefined.
总结:同一个查询的结果在不同的MySQL版本之间可能会发生变化,也可能不会发生变化。不能保证查询在任何给定的 MySQL 版本中都能按预期工作。
关于mysql - 此 sql 脚本在不同的 MySQL 服务器版本上返回不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27551266/