简体   繁体   中英

Using outer query value in sub-query - Getting error : “no such column … ”

I am grouping my outer query based on a column. I then want to use the value from the specific column in an inner query that is performing some other calculation using that column's value.

I get the error "No such column .... "

Here is my query:

SELECT 
    language AS outer_language, <--- This is the value I want to use
    COUNT(*) / (SELECT 
            COUNT(*)
        FROM
            e_lan_specs AS e
                INNER JOIN
             ue_lan_info_spec AS ue ON ue.lan_specs_id = e.id
                INNER JOIN
            u_lan_info AS u ON u.id = ue.lan_info_id
        WHERE
            u.language = outer_language) as perc_avg,
    first,
    second,
    third,
    fourth
FROM
    e_lan_specs AS e
        INNER JOIN
     ue_lan_info_spec AS ue ON ue.lan_specs_id = e.id
        INNER JOIN
    u_lan_info AS u ON u.id = ue.lan_info_id
GROUP BY first , second , third , fourth , language

You can't do that. You have to use language with the appropriate alias. Something like:

SELECT language AS outer_language, <--- This is the value I want to use
       COUNT(*) / (SELECT COUNT(*)
                   FROM e_lan_specs AS e2 INNER JOIN
                        ue_lan_info_spec AS ue2
                        ON ue2.lan_specs_id = e2.id INNER JOIN
                        u_lan_info AS u2
                        ON u2.id = ue2.lan_info_id
                   WHERE u2.language = u.outer_language
                  ) as perc_avg,
       . . .

The problem is that the select clause is what gives the language column its alias as outer_language. You can't use the alias within the same statement that creates it.

Try:

SELECT language AS outer_language, <--- This is the value I want to use
    COUNT(*) / (SELECT 
            COUNT(*)
        FROM
            e_lan_specs AS e
                INNER JOIN
             ue_lan_info_spec AS ue ON ue.lan_specs_id = e.id
                INNER JOIN
            u_lan_info AS u2 ON u2.id = ue.lan_info_id
        WHERE
            u.language = u2.language) as perc_avg,
    first,
    second,
    third,
    fourth
FROM
    e_lan_specs AS e
        INNER JOIN
     ue_lan_info_spec AS ue ON ue.lan_specs_id = e.id
        INNER JOIN
    u_lan_info AS u ON u.id = ue.lan_info_id
GROUP BY first , second , third , fourth , language

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