2012-04-17 71 views
0

有什麼辦法可以在你的表名上使用「order」函數。即我想要將兩個表合併,然後按一個列進行排序,然後按表名稱進行排序。按表名排序?

回答

9

將常數添加到描述表名的列列表中,例如,

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

感謝的人。正是我在找什麼。 – 2012-04-17 18:55:11

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;