我试图选择q1.res
,这将是FROM
子查询的结果:
(SELECT SUM(gwr.res1)/(COUNT(*)*1000) as 'res'
FROM process.gwr
WHERE started = 1 AND gwr.user2 = g.id AND gwr.loss = g.id) AS q1
问题是我试图在子查询中选择
g.id
,但是由于某种原因它无法访问它并返回错误消息:[Err] 1054 - Unknown column 'g.id' in 'where clause'
这是没有意义的,因为g.id(
grand.id
具有id
列)SELECT
g.name, g.point, g.mx, q1.res
FROM
(SELECT SUM(gwr.res1)/(COUNT(*)*1000) as 'res'
FROM process.gwr
WHERE (started = 1) AND (gwr.user2 = g.id) AND (gwr.loss = g.id)) AS q1, grand g
LEFT JOIN process.users p ON (p.id = g.owner)
WHERE
p.name != ""
ORDER BY
g.point DESC,
g.mx DESC
LIMIT 5
问题出在哪里?
最佳答案
尝试从子查询的'res'中删除''。
SELECT
g.name, g.point, g.mx, q1.res
FROM
(SELECT SUM(gwr.res1)/(COUNT(*)*1000) as res FROM process.gwr WHERE started = 1 AND gwr.user2 = g.id AND gwr.loss = g.id) AS q1, grand g
LEFT JOIN process.users p ON (p.id = g.owner)
WHERE
p.name != ""
ORDER BY
g.point DESC,
g.mx DESC
LIMIT 5
关于mysql - 选择子查询的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21141749/