简体   繁体   English

SQL中的“无效的列名”错误

[英]“Invalid column name” error in SQL

What's wrong with this code? 此代码有什么问题?

I get this error: 我收到此错误:

Invalid column name 'Distance' 无效的列名“距离”

Code: 码:

SELECT 
    Company.CompanyId as Id,
    ( 6371  * acos( cos( radians(47.8423155) ) * cos( radians( Company.Latitude  ) ) * cos( radians( Company.Longitude  ) - radians(35.232933) ) + sin( radians(47.8423155) ) * sin( radians( Company.Latitude ) ) ) ) AS Distance
FROM 
    Company 
INNER JOIN  
    Product ON Company.CompanyId = Product.CompanyId 
WHERE
    Distance< 5000   
ORDER BY
    Distance

Depending on the you're using, some RDBMSs don't allow referring to column aliases in the where and order by columns. 根据您使用的 ,某些RDBMS不允许在whereorder by列中引用列别名。 Just use the actual column names: 只需使用实际的列名:

SELECT      Company.CompanyId as Id,
            Company.VisitCount AS myVisitCount 
FROM        Company
INNER JOIN  Product ON  Company.CompanyId = Product.CompanyId 
WHERE       Company.VisitCount < 5000   
ORDER BY    Company.VisitCount

Wrap the select inside subselect 将选择内容包装在子选择内

   select * from(SELECT 
    Company.CompanyId as Id,
    ( 6371  * acos( cos( radians(47.8423155) ) * cos( radians( Company.Latitude  ) ) * cos( radians( Company.Longitude  ) - radians(35.232933) ) + sin( radians(47.8423155) ) * sin( radians( Company.Latitude ) ) ) ) AS Distance
FROM 
    Company 
INNER JOIN  
    Product ON (Company.CompanyId = Product.CompanyId) 
) AS P
WHERE
    P.Distance< 5000   
ORDER BY
    P.Distance;

The inner subquery returns the function on basis of which you are filtering with the alias Distance. 内部子查询返回使用别名Distance进行过滤的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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