简体   繁体   中英

join two inline tables - unknown column?

I'm getting "Error Code: 1054. Unknown column 'E1.extensao' in 'field list'". I changed table names but my query is as follows:

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

I'm more used to SQL Server, not MySQL. What am I getting wrong?

I assume that E1.extensao means E1.boughtEnough ? Look closely at how E1 is defined:

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

There's a t.boughtEnough , but you're not "passing it up" to E1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM