看如下例子
mysql> select count(1),task_type,status from action where biz_date='20141014' group by status;
+----------+-----------+--------+
| count(1) | task_type | status |
+----------+-----------+--------+
| 1 | shell | fail |
| 16 | shell | succ |
+----------+-----------+--------+
2 rows in set (0.00 sec)
mysql> select count(1),task_type,status from action where biz_date='20141014' group by task_type,status;
+----------+-----------+--------+
| count(1) | task_type | status |
+----------+-----------+--------+
| 2 | odps_sql | succ |
| 1 | shell | fail |
| 14 | shell | succ |
+----------+-----------+--------+
3 rows in set (0.00 sec)
mysql> select count(1),task_type from action where biz_date='20141014' group by status;
+----------+-----------+
| count(1) | task_type |
+----------+-----------+
| 1 | shell |
| 16 | shell |
+----------+-----------+
2 rows in set (0.00 sec)
这在mysql 中是合法的。由于group by 和select 列不一致,在ORACLE中会报错。
参考 http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
得知这是mysql的设计,当group by 字段和select 多字段是唯一的一个分组时,这样能提高效率。
但是如果不能唯一确定的话,mysql会随机选一个值。这是要注意的。
个人觉的这更是个坑。。
这个功能是MySQL对标准sql的扩展,使用的时候要注意