Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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 5.6 InnoDB count、group by、having和order by返回错误结果_Mysql_Count_Group By_Innodb_Having - Fatal编程技术网

MySQL 5.6 InnoDB count、group by、having和order by返回错误结果

MySQL 5.6 InnoDB count、group by、having和order by返回错误结果,mysql,count,group-by,innodb,having,Mysql,Count,Group By,Innodb,Having,我对一个大查询有问题,但试图简化它,发现类似的奇怪行为: select concat(a.col1,a.col2) as b, count(a.id) as c from test as a group by a.id having b = "644591" order by b 同一查询在5.6 InnoDB上不返回结果,但5.5 MyISAM返回一个正确的匹配项 如果删除“order by b”,它在InnoDB上也会返回correct 表: CREATE TABLE `te

我对一个大查询有问题,但试图简化它,发现类似的奇怪行为:

select concat(a.col1,a.col2) as b,
       count(a.id) as c
from test as a
group by a.id
having b = "644591"
order by b
同一查询在5.6 InnoDB上不返回结果,但5.5 MyISAM返回一个正确的匹配项

如果删除“order by b”,它在InnoDB上也会返回correct

表:

CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `col1` varchar(100) NOT NULL DEFAULT '',
  `col2` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

id|col1    |col2
 1|        |644591
 2|70083531|1226109
那么

select concat(a.col1,a.col2) as b,
       count(a.id) as c
from test a
where concat(a.col1,a.col2) = '644591'
group by concat(a.col1,a.col2)
order by b;

字符串应该用单引号引起来:b='644591'@Jens-你能详细解释一下为什么会这样吗?@草莓From:“如果启用了ANSI_quotes SQL模式,字符串文字只能用单引号引起来,因为双引号内的字符串被解释为标识符。”@Jens还好-但是为什么这样做有效<代码>选择concat(a.col1,a.col2)作为b,从测试a中计算(a.id)作为c,其中concat(a.col1,a.col2)=“644591”按concat(a.col1,a.col2)分组按b排序它在having上使用单引号生成相同的结果问题的原因是没有返回正确行的大查询已经具有where和grouping,然后需要对结果应用having filter。像这样的简单查询应该在innodb和myisam上返回相同的结果。