mysql_group by 奇怪问题解决

我回去查了一下关于mysql group by 奇怪的现像,
    可以通过修改sql_mode 的方法改变这种奇怪的现像。
    例如下:

mysql> SELECT job,sal,deptno
    -> FROM emp
    -> GROUP by deptno;
+----------+---------+--------+
| job      | sal     | deptno |
+----------+---------+--------+
| MANAGER  | 2450.00 |     10 |
| CLERK    |  800.00 |     20 |
| SALESMAN | 1600.00 |     30 |
+----------+---------+--------+
3 rows in set (0.04 sec)

mysql> set sql_mode = "STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY";
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT job,sal,deptno
    -> FROM emp
    -> GROUP by deptno;
ERROR 1055 (42000): 'tao.emp.JOB' isn't in GROUP BY
mysql> SELECT count(job),count(sal),deptno
    -> FROM emp
    -> GROUP BY deptno;
+------------+------------+--------+
| count(job) | count(sal) | deptno |
+------------+------------+--------+
|          3 |          3 |     10 |
|          5 |          5 |     20 |
|          6 |          6 |     30 |
+------------+------------+--------+
3 rows in set (0.00 sec)


//-------------------------------------------------
//以上修改sql_mode 只在当前会话中生效.
//如果需要长期有效请修改 my.ini 文件中

#sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
sql_mode = "STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY"

你可能感兴趣的:(group by)