分组查询时,select的字段是否一定要都在group by中?
CREATE TABLE `student1` (
`id` int(11) NOT NULL COMMENT '学号',
`name` varchar(60) NOT NULL COMMENT '姓名',
`birth` date NOT NULL COMMENT '出生日期',
`sex` varchar(1) DEFAULT NULL,
`age` int(11) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
insert into student values(1,'Tom','1998-10-01','男',23,96),(2,'Jim','1997-07-04','男',24,95),(3,'Lily','1999-11-12','女',21,99),(4,'Lilei','1996-09-21','男',25,90),(5,'Lucy','1999-12-02','女',21,93),(6,'Jack','1988-04-27','男',32,89),(7,'Liam','1991-09-08',' 男',28,100);
mysql> select * from student;
+----+-------+------------+------+-----+-------+
| id | name | birth | sex | age | score |
+----+-------+------------+------+-----+-------+
| 1 | Tom | 1998-10-01 | 男 | 23 | 96 |
| 2 | Jim | 1997-07-04 | 男 | 24 | 95 |
| 3 | Lily | 1999-11-12 | 女 | 21 | 99 |
| 4 | Lilei | 1996-09-21 | 男 | 25 | 90 |
| 5 | Lucy | 1999-12-02 | 女 | 21 | 93 |
| 6 | Jack | 1988-04-27 | 男 | 32 | 89 |
| 7 | Liam | 1991-09-08 | 男 | 28 | 100 |
+----+-------+------------+------+-----+-------+
7 rows in set (0.00 sec)
mysql> select id,name,score from student where score >95 group by id,name,score;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Tom | 96 |
| 3 | Lily | 99 |
| 7 | Liam | 100 |
+----+------+-------+
3 rows in set (0.01 sec)
mysql> select id,name,score from student where score >95 group by score;
ERROR 1055 (42000): Expression #1 of
SELECT list is not in GROUP BY clause
and contains nonaggregated column
'test.student.id' which is not functionally
dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
mysql> select id,name,score from student where score >95 group by name;
ERROR 1055 (42000): Expression #1 of
SELECT list is not in GROUP BY clause
and contains nonaggregated column
'test.student.id' which is not functionally
dependent on columnsin GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
mysql> select id,name,score from student where score >95 group by id;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Tom | 96 |
| 3 | Lily | 99 |
| 7 | Liam | 100 |
+----+------+-------+
3 rows in set (0.00 sec)
SELECT name, address, MAX(age) FROM t GROUP BY name;
The query is valid if name is a primary key of t or is a unique NOT NULL column. In such cases,MySQL recognizes that the selected column is functionally dependent on a grouping column. Forexample, if name is a primary key, its value determines the value of address because each group has only one value of the primary key and thus only one row. As a result, there is no randomness in the choice of address value in a group and no need to reject the query.
The query is invalid if name is not a primary key of t or a unique NOT NULL column.
alter table student add unique(name);
mysql> select id,name,score from student where score >95 group by name;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 7 | Liam | 100 |
| 3 | Lily | 99 |
| 1 | Tom | 96 |
+----+------+-------+
3 rows in set (0.00 sec)
mysql> select @ ;
+-------------------------------------------------------------------------------------------------------------------------------------------+| @
|+-------------------------------------------------------------------------------------------------------------------------------------------+
| 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 = sys.list_drop(@@sql_mode, 'ONLY_FULL_GROUP_BY');
Query OK, 0 rows affected (0.05 sec)
mysql> select id,name,score from student where score >95 group by score;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Tom | 96 |
| 3 | Lily | 99 |
| 7 | Liam | 100 |
+----+------+-------+
3 rows in set (0.00 sec)
SET @@sql_mode = sys.list_add(@@sql_mode, 'ONLY_FULL_GROUP_BY');
mysql> select id,name,score from student where score >95 group by score;
ERROR 1055 (42000): Expression #1 of
SELECT list is not in GROUP BY clause
and contains nonaggregated column
'test.student.id' which is not functionally
dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by。
评论