Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在列别名之后立即使用它';在SELECT子句中指定了什么?_Mysql_Sql_Column Alias - Fatal编程技术网

Mysql 如何在列别名之后立即使用它';在SELECT子句中指定了什么?

Mysql 如何在列别名之后立即使用它';在SELECT子句中指定了什么?,mysql,sql,column-alias,Mysql,Sql,Column Alias,我在第7行创建了一个名为total_city_pop的列别名 然后,在第8行中,我尝试在新的计算中使用该别名 但是,我收到一条错误消息,说“total_city_pop”列不存在第8行:(ci.city_property_pop+ci.metroarea_pop)/total_city_pop AS…^” 为什么创建别名后不能使用它 这是代码-- 谢谢。您需要重复子查询中的表达式。但是,我建议您使用COALESCE()简化表达式: MySQL的最新版本支持计算列。这将允许您将计算放在表的定义中。

我在第7行创建了一个名为total_city_pop的列别名

然后,在第8行中,我尝试在新的计算中使用该别名

但是,我收到一条错误消息,说“total_city_pop”列不存在第8行:(ci.city_property_pop+ci.metroarea_pop)/total_city_pop AS…^”

为什么创建别名后不能使用它

这是代码--


谢谢。

您需要重复子查询中的表达式。但是,我建议您使用
COALESCE()
简化表达式:


MySQL的最新版本支持计算列。这将允许您将计算放在表的定义中。

您需要在子查询中重复该表达式。但是,我建议您使用
COALESCE()
简化表达式:


MySQL的最新版本支持计算列。这将允许您将计算放在表的定义中。

您不能。您需要使用子查询或重复表达式。您可能希望读取,但无法读取。您需要使用子查询或重复表达式。您可能希望
SELECT co.code, ci.name AS capital_city, ci.city_proper_pop, ci.metroarea_pop, ci.urbanarea_pop,
    CASE
        WHEN (ci.city_proper_pop + ci.metroarea_pop + ci.urbanarea_pop) IS NULL
        THEN (ci.city_proper_pop + ci.urbanarea_pop)
        ELSE (ci.city_proper_pop + ci.metroarea_pop + ci.urbanarea_pop) END AS total_city_pop, 
    (ci.city_proper_pop + ci.metroarea_pop) / total_city_pop AS percent_outside_urbanarea 
FROM countries AS co 
    INNER JOIN cities AS ci ON co.capital = ci.name 
    WHERE continent LIKE '%America%' OR continent LIKE 'Europe'     
    ORDER BY total_city_pop DESC 
    LIMIT 10;
SELECT co.code, ci.name AS capital_city, ci.city_proper_pop, ci.metroarea_pop, ci.urbanarea_pop,
       (ci.city_proper_pop + ci.metroarea_pop + COALECE(ci.urbanarea_pop, 0) ) as  total_city_pop, 
       (ci.city_proper_pop + ci.metroarea_pop) / (ci.city_proper_pop + ci.metroarea_pop + COALECE(ci.urbanarea_pop, 0) ) AS percent_outside_urbanarea 
FROM countries co INNER JOIN
     cities ci
     ON co.capital = ci.name 
WHERE continent LIKE '%America%' OR continent LIKE 'Europe'     
ORDER BY total_city_pop DESC 
LIMIT 10;