在子查询中使用外部查询值 - 获取错误:“没有这样的列......”

时间:2014-07-09 16:06:32

标签: mysql sql sqlite

我根据列对外部查询进行分组。然后,我想使用内部查询中特定列的值,该查询使用该列的值执行其他计算。

我收到错误“没有这样的栏目......”

这是我的问题:

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

2 个答案:

答案 0 :(得分:1)

你做不到。您必须使用language和相应的别名。类似的东西:

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,
       . . .

答案 1 :(得分:0)

问题是select子句赋予语言列别名为outer_language的内容。您无法在创建它的同一语句中使用别名。

尝试:

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