简体   繁体   English

选择使用列别名的查询

[英]Select Query using Column Alias Name

I have a query like : 我有一个查询,如:

Select table1.Name AS aliasname1, Count(aliasname1) as aliasname2 from table1.

But i am not sure if this query will execute successfully in sql because i have used alias name in same select statement .I need a solution for doing the same in sql. 但是我不确定此查询是否将在sql中成功执行,因为我在同一select语句中使用了别名。我需要在sql中执行相同操作的解决方案。 I want solution as 我想要解决方案

aliasname1   aliasname2

Name1         4
Name2         4
Name3         4
Name4         4

You can't reference an alias in your SELECT statement like that. 您不能像这样在SELECT语句中引用别名。 You are creating and attempting to call an alias at the same time, so the compiler doesn't know what the aliasname1 is when you reference it in the same select statement. 您正在创建并尝试同时调用别名,因此当您在同一select语句中引用aliasname1时,编译器将不知道aliasname1是什么。 So in order to fix this, you'd have to write the query the following way: 因此,为了解决此问题,您必须按以下方式编写查询:

select 
  table1.Name AS aliasname1, 
  Count(table1.Name) as aliasname2 
from table1

Or if you want to reference an alias, you'll need to use a subquery: 或者,如果您要引用别名,则需要使用子查询:

select aliasname1,
  count(aliasname1) as aliasname2 
from 
(
  select table1.Name AS aliasname1
  from table1
) as d

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

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