SQL 错误 [1055] [42000]: Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'rlstudy.s.SCORE' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
时间: 2023-07-21 08:02:14 浏览: 155
这个错误是由于 MySQL 的 `ONLY_FULL_GROUP_BY` 模式引起的。在这个模式下,当使用 GROUP BY 子句进行分组后,SELECT 语句中的 ORDER BY 子句必须只包含分组的列或者使用聚合函数。
要解决这个问题,你可以采取以下两种方法之一:
1. 添加 `sc.SCORE` 到 GROUP BY 子句中(如果你希望按照 `sc.SCORE` 进行分组):
```sql
SELECT s.*
FROM student s
INNER JOIN sc ON sc.SNO = s.SNO
INNER JOIN course c ON c.CNO = sc.CNO
INNER JOIN teacher t ON t.TNO = c.TNO
WHERE t.TNAME = '张三'
GROUP BY s.SNO, s.SNAME, ...
ORDER BY sc.SCORE DESC
LIMIT 1, 1
```
2. 使用聚合函数来替代 ORDER BY 子句中的 `sc.SCORE`:
```sql
SELECT s.*
FROM student s
INNER JOIN sc ON sc.SNO = s.SNO
INNER JOIN course c ON c.CNO = sc.CNO
INNER JOIN teacher t ON t.TNO = c.TNO
WHERE t.TNAME = '张三'
GROUP BY s.SNO, s.SNAME, ...
ORDER BY MAX(sc.SCORE) DESC
LIMIT 1, 1
```
上述方法中,`s.SNO, s.SNAME, ...` 代表需要根据实际情况列出 student 表中的所有需要 GROUP BY 的列。使用聚合函数 MAX 来代替 `sc.SCORE`,确保 ORDER BY 子句符合要求。
阅读全文