我想选择平均超时率低于50%的所有用户:
SELECT *
FROM user u
WHERE (SELECT Avg(gu.didtimeout)
FROM (SELECT didtimeout
FROM gameuser
WHERE userid = u.userid
LIMIT 50) gu) < 0.5;
但是,我收到此错误:
ERROR 1054 (42S22): Unknown column 'u.userID' in 'where clause'
我如何在合法的MySQL中进行此查询?
编辑1:
以下是GameUser
和User
架构的相关部分。
CREATE TABLE `User` (
`userID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userID`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=latin1;
CREATE TABLE `GameUser` (
`gameID` mediumint(8) unsigned NOT NULL,
`userID` mediumint(8) unsigned NOT NULL,
`didTimeout` tinyint(1) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
编辑2:
使用MySQL 5.7.11。
答案 0 :(得分:4)
要获取用户ID,请执行以下操作:
SELECT gu.userid
FROM gameuser gu
GROUP BY gu.userid
HAVING AVG(gu.didtimeout) < 0.5;
如果需要,您可以加入其他用户信息。
答案 1 :(得分:3)
SELECT * FROM user WHERE userid in (SELECT gu.userid
FROM gameuser gu
GROUP BY gu.userid
HAVING AVG(gu.didtimeout) < 0.5);
答案 2 :(得分:0)
我没有评论的声誉,但是聚合函数的产品不能在WHERE子句中使用,而是使用HAVING。
SELECT Avg(gu.didtimeout) as average_timeout
FROM user u
JOIN gameuser gu
ON gu.userid = u.userid
GROUP BY u.userid
HAVING average_timeout < .5
LIMIT 50;