选择子查询的结果



我正在尝试选择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.idid列)

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

您需要提及数据库对象"g"所指的内容。这意味着,您需要将其指定为"FROM"部分中的表(或视图)作为纯交叉引用,或者 - 如果您愿意 - 然后使用某种连接。

例如

SELECT SUM(gwr.res1)/(COUNT(*)*1000) as 'res' 
FROM process.gwr, process.g 
WHERE started = 1 
    AND gwr.user2 = g.id 
    AND gwr.loss = g.id 

请注意"FROM"部分中的"process.g"。

希望有帮助。

K

相关内容

  • 没有找到相关文章

最新更新