按表名排序?

时间:2012-04-17 18:45:47

标签: sql

有没有办法在表名上使用“order”功能。即我希望将两个表合并,然后按一列排序,然后按表名排序。

2 个答案:

答案 0 :(得分:9)

将常量添加到描述表名的列列表中,例如

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

答案 1 :(得分:0)

您可以创建一个单独的列(假设您的列为col1col2

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;