参考地址: http://blog.csdn.net/ccccalculator/article/details/70432123
因为在mysql中使用group by 是总是出现1055的错误,这就导致了必须去查看是什么原因了,查询了相关的资料,现在将笔记记录下来,以便后面可以参考使用:
sql_mode:简而言之就是:它定义了你MySQL应该支持的sql语法,对数据的校验等等
select @@sql_mode:使用该命令我们可以查看我们当前数据库的sql_mode
mysql> select @@sql_mode; +-------------------------------------------------------------------------------------------------------------------------------------------+ | @@sql_mode | +-------------------------------------------------------------------------------------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +-------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
下面我们来看看sql_mode的各个值的含义:
ONLY_FULL_GROUP_BY:
对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么将认为这个SQL是不合法的,因为列不在GROUP BY从句中
因为有only_full_group_by,所以我们要在MySQL中正确的使用group by语句的话,只能是select column1 from tb1 group by column1(即只能展示group by的字段,其他均都要报1055的错)
实例:
mysql> select * from tt1; +----+-------+--------+ | id | name | gender | +----+-------+--------+ | 1 | xiong | 0 | | 2 | ying | 0 | | 3 | cai | 0 | | 4 | zhang | 0 | | 5 | li | 1 | | 6 | wang | 1 | +----+-------+--------+ 6 rows in set (0.00 sec) mysql> select id,name from tt1 group by name; ERROR 1055 (42000):
mysql> select * from tt1; +----+-------+--------+ | id | name | gender | +----+-------+--------+ | 1 | xiong | 0 | | 2 | ying | 0 | | 3 | cai | 0 | | 4 | zhang | 0 | | 5 | li | 1 | | 6 | wang | 1 | +----+-------+--------+ 6 rows in set (0.00 sec) mysql> select * from tt1 group by name; ERROR 1055 (42000):
mysql> select * from tt1; +----+-------+--------+ | id | name | gender | +----+-------+--------+ | 1 | xiong | 0 | | 2 | ying | 0 | | 3 | cai | 0 | | 4 | zhang | 0 | | 5 | li | 1 | | 6 | wang | 1 | +----+-------+--------+ 6 rows in set (0.00 sec) mysql> select name from tt1 group by name; +-------+ | name | +-------+ | cai | | li | | wang | | xiong | | ying | | zhang | +-------+ 6 rows in set (0.00 sec)
所以我们要使用能正确的使用group by 的话就必须删除掉only_full_group_by
set sql_mode=(select replace(@@sql_mode,‘ONLY_FULL_GROUP_BY‘,‘‘)); 可以使用该语句来将空格替换掉only_full_group_by,这样我们就可以使用
mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,‘ONLY_FULL_GROUP_BY‘,‘‘)); Query OK, 0 rows affected (0.00 sec) mysql> select id,name from tt1 group by name; +----+-------+ | id | name | +----+-------+ | 3 | cai | | 5 | li | | 6 | wang | | 1 | xiong | | 2 | ying | | 4 | zhang | +----+-------+ 6 rows in set (0.00 sec)
mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,‘ONLY_FULL_GROUP_BY‘,‘‘)); Query OK, 0 rows affected (0.00 sec) mysql> select * from tt1 group by name; +----+-------+--------+ | id | name | gender | +----+-------+--------+ | 3 | cai | 0 | | 5 | li | 1 | | 6 | wang | 1 | | 1 | xiong | 0 | | 2 | ying | 0 | | 4 | zhang | 0 | +----+-------+--------+ 6 rows in set (0.00 sec)
但是这种方法只是做了暂时的修改,我们可以更改配置文件my.ini
sql_mode= "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"