Chris Stewart
Chris Stewart

Reputation: 1689

Order by tablename?

Is there any way to use the "order" function on your table name. i.e I want to union two tables then sort by the one column, then by table name.

Upvotes: 1

Views: 1909

Answers (2)

user554546
user554546

Reputation:

You can create a separate column (assuming that your columns are col1 and col2)

select col1,col2,table_1 as table_name
from table_1
union
select col1,col2,table_2 as table_name
from table_2
order by col1,table_name;

Upvotes: 0

jrummell
jrummell

Reputation: 43097

Add constant to your column list that describes your table name, E.g.

select *, 'TableA' as TableName
from TableA
union all
select *, 'TableB' as TableName
from TableB
order by TableName

Upvotes: 10

Related Questions