1

我有一个球员表现表,我想返回每个球员的高分,如果他们在达到这个分数时没有出局,则在其后添加一个星号 ('*')。这是我到目前为止构建的查询,但它给了我错误 #1111: group 函数的无效使用

 SELECT player_id, players.name,
 COUNT( * ) AS matches, 
 SUM( score ) AS runs,
(SELECT IF(is_out = 0, CONCAT(MAX(score),'*'), MAX(score)) FROM batting WHERE score = MAX(score) ) AS high
 FROM batting
 RIGHT JOIN players ON batting.player_id = players.p_id
 WHERE player_id <> 0
 GROUP BY player_id

我看过其他时候出现这个错误,它应该表明何时通过 MAX/MIN 函数执行 SUM 函数,但我看不到在我的查询中发生这种情况

我哪里错了,我将如何纠正这个问题,grma

4

2 回答 2

1

选择 IF(is_out = 0, CONCAT(MAX(score),'*'), MAX(score)) FROM batting WHERE score = MAX(score)

这没有任何意义(您不能在 WHERE 子句中使用聚合函数)。事实上,我很难想象你期望它做什么。MySQL 可能也很困惑。

于 2015-12-23T14:04:12.090 回答
1

试试这个,即添加您在 GROUP BY 子句中选择的列名( player_id,players.name ):

SELECT player_id, players.name,
 COUNT( * ) AS matches, 
 SUM( score ) AS runs,
(SELECT IF(is_out = 0, CONCAT(MAX(score),'*'), MAX(score)) FROM batting WHERE score = MAX(score) ) AS high
 FROM batting
 RIGHT JOIN players ON batting.player_id = players.p_id
 WHERE player_id <> 0
 GROUP BY player_id, players.name, high
于 2015-12-23T13:53:59.290 回答