加入两个内联表 - 未知列?

时间:2011-11-29 19:58:45

标签: mysql sql select

我得到"错误代码:1054。未知栏' E1.extensao'在'字段列表'"。我更改了表名,但我的查询如下:

SELECT E1.stateName, E1.city, E1.boughtEnough, E2.bought
FROM 
    (SELECT count(DISTINCT idClient),city,stateName FROM
        (SELECT
            max(n.ordervalue) as boughtEnough,
            m.idClient,
            i.nome AS city,
            i.id AS cityId,
            i.stateName
        FROM Clients c
        INNER JOIN client_order m ON c.idClient = m.idClient
        INNER JOIN orders n ON m.client_order = n.client_order
        INNER JOIN orderDetail p ON n.idorder = p.idorder
                                AND p.idCurso = m.idCurso
        INNER JOIN cities i ON c.city = i.id

        WHERE
            m.idCurso = '10'
        GROUP BY 
            m.idClient,
            i.nome,
            i.id,
            i.stateName
        HAVING max(n.orders) >= 6) t
    GROUP BY t.city, t.stateName
    ORDER BY t.stateName,t.city) E1
JOIN (SELECT count(DISTINCT idClient),city,stateName FROM
        (SELECT
            count(n.ordervalue) as bought,
            m.idClient,
            i.nome AS city,
            i.id AS cityId,
            i.stateName
        FROM Clients c
        INNER JOIN client_order m ON c.idClient = m.idClient
        INNER JOIN orders n ON m.client_order = n.client_order
        INNER JOIN orderDetail p ON n.idorder = p.idorder
                                AND p.idCurso = m.idCurso
        INNER JOIN cities i ON c.city = i.id

        WHERE
            m.idCurso = '10'
        GROUP BY 
            m.idClient,
            i.nome,
            i.id,
            i.stateName
        HAVING ((max(n.orders) < 6) AND (count(n.orders) >= 1))) t
    GROUP BY t.city, t.stateName
    ORDER BY t.stateName,t.city) E2 ON E1.cityId = E2.cityId

我更习惯于SQL Server,而不是MySQL。我错了什么?

1 个答案:

答案 0 :(得分:3)

我认为E1.extensao表示E1.boughtEnough?仔细查看E1的定义方式:

    (SELECT count(DISTINCT idClient),city,stateName FROM
        (SELECT
            max(n.ordervalue) as boughtEnough,
        ...) t
    ...) E1

有一个t.boughtEnough,但你不是“把它传递”到E1