Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Alias names for a table

Tags:

sql

sql-server

Can we have multiple alias names for a single table?

like image 848
Deviprasad Das Avatar asked Jul 16 '10 10:07

Deviprasad Das


2 Answers

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]
like image 147
ConcernedOfTunbridgeWells Avatar answered Oct 31 '22 21:10

ConcernedOfTunbridgeWells


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.

like image 30
Chris Diver Avatar answered Oct 31 '22 21:10

Chris Diver