SQL 一个表的多个别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:51:27  来源:igfitidea点击:

Multiple Alias names for a table

sqlsql-server

提问by Deviprasad Das

Can we have multiple alias names for a single table?

一个表可以有多个别名吗?

回答by ConcernedOfTunbridgeWells

Yes. You need to do this for a self join,for example f you have a table storing a hierarchy:

是的。您需要为自联接执行此操作例如,如果您有一个存储层次结构的表:

create table Foo (
    FooID int
   ,ParentFooID int
   ,[columns]
)

You can do a join to get the children of parents that satisfy a particular condition with a query like:

您可以使用以下查询进行连接以获取满足特定条件的父母的孩子:

Select b.*
  from Foo a
  join Foo b
    on a.FooID = b.ParentFooID
   and [some condition filtering a]

回答by Chris Diver

No, not on the same table, but you can select the same table twice and give each a different alias.

不,不是在同一张桌子上,但是您可以两次选择同一张桌子并给每个桌子赋予不同的别名。

SELECT alias1.*, alias2.*
FROM mytable alias1, mytable alias2

This will then allow you to use the same table for a different purpose within a single query.

这将允许您在单个查询中将同一个表用于不同的目的。