MySql出现错误:ERROR 1055 (42000) 和 MYSQL的WARNINGS 和 ERRORS查询细节
mysql> select * from (select * from obj_questions_records order by utime desc ) as R group by user_id,uuid order by utime desc;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'R.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
ERROR 1055 (42000):
mysql> show warnings;
+-------+------+---------+
| Level | Code | Message |
+-------+------+---------+
| Error | 1055 | |
+-------+------+---------+
1 row in set (0.00 sec)
mysql> show errors;
+-------+------+---------+
| Level | Code | Message |
+-------+------+---------+
| Error | 1055 | |
+-------+------+---------+
1 row in set (0.00 sec)
mysql>
(2). 问题原因: mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)
2) 查看sql_model参数命令:
mysql> SELECT @@GLOBAL.sql_mode;
+------------------------------------------------------------------------------------------------------------------------+
| @@GLOBAL.sql_mode |
+------------------------------------------------------------------------------------------------------------------------+
| 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)
mysql> SELECT @@SESSION.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@SESSION.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)
发现: mysql> set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' ;
Query OK, 0 rows affected (0.00 sec)
2) 然后就可以成功执行啦,如下:
mysql> select * from objective_questions_records order by utime desc
-> ;
+------+---------+----------------------------------+---------------------+----------+
| id | user_id | uuid | utime | is_right |
+------+---------+----------------------------------+---------------------+----------+
| 2145 | 548126 | 828a333 | 2017-08-12 11:20:36 | 6 |
| 2344 | 334243 | 3342432weowwej | 2017-08-12 11:19:27 | 4 |
| 1214 | 548126 | 828a333 | 2017-08-12 10:36:28 | 0 |
| 2133 | 1234545 | 29320a8813514a5bb5a28a605ddbc5a2 | 2017-08-08 10:38:09 | 3 |
| 1215 | 334243 | 8aac4907508d5d3d0150bbefc20e4ba5 | 2017-08-06 10:36:14 | 1 |
| 2921 | 334243 | 8aac4907508d5d3d0150bbefc20e4ba5 | 2017-08-02 10:36:27 | 0 |
| 1232 | 3748549 | 29320a8813514a5bb5a28a605ddbc5a2 | 2017-07-04 10:39:24 | 2 |
| 2134 | 334243 | 29320a8813514a5bb5a28a605ddbc5a2 | 2017-06-14 10:38:12 | 5 |
| 2919 | 334243 | 29320a8813514a5bb5a28a605ddbc5a2 | 2016-03-15 18:12:25 | 1 |
+------+---------+----------------------------------+---------------------+----------+
9 rows in set (0.00 sec)
注意,此时的修改可能是临时的,要想彻底解决这个问题,网上说,可以修改MySQL的配置文件,参见
def get_Records(self, sql):
try:
self.conn_get_records.connect()
set_sql_mode='''
set sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
'''
self.conn_get_records.execute(set_sql_mode)
sql='''select * from (select * from obj_questions_records order by utime desc ) as R group by user_id,uuid order by utime desc'''
excute_result=self.conn_get_records.execute(sql)
return excute_result
except:
print sql
return 0
#exit(1)
finally:
self.conn_get_records.close()
(5) 其他显示sql错误的MySQL命令介绍
mysql> show dfdafsadf;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dfdafsadf' at line 1
这里就有个错误。关于如何显示她,已经很明显了。
mysql> show errors;
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Error | 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dfdafsadf' at line 1 |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show errors \G;
*************************** 1. row ***************************
Level: Error
Code: 1064
Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dfdafsadf' at line 1
1 row in set (0.00 sec)
ERROR:
No query specified
如果一下子有好多错误,而你又想只显示第二条的话:
mysql> show count(*) errors;
+-----------------------+
| @@session.error_count |
+-----------------------+
| 1 |
+-----------------------+
1 row in set (0.00 sec)
mysql> show count(1) errors;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1) errors' at line 1
注意:这里的count(*)不能写成count(1).
mysql> select @@error_count;
+---------------+
| @@error_count |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
SHOW WARNINGS 显示上一个语句的错误、警告以及注意。
mysql> show dfdafsadf;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dfdafsadf' at line 1
mysql> show errors;
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Error | 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dfdafsadf' at line 1 |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show warnings;
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Error | 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dfdafsadf' at line 1 |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
参考网址:
http://blog.chinaunix.net/uid-259788-id-2139240.html
http://blog.csdn.net/u011409644/article/details/73611102
http://blog.csdn.net/u283056051/article/details/52463948
http://www.linuxidc.com/Linux/2017-08/146516.htm