从SQL Server中的多个表中获取不同的列值

时间:2013-09-14 09:55:42

标签: sql sql-server

我在SQL Server表中有以下数据:

id  Sal
1   100
2   200

id  Wages
1   600
2   800

我希望输出如下:

id   Sal/Wages
1    100
1    600
2    200
2    800

如何在SQL Server中使用SELECT语句?

2 个答案:

答案 0 :(得分:2)

使用UNION ALL

Select Id, sal as [sal/wages]
from table1
UNION ALL
Select Id, wages as [sal/wages]
from table2
Order by id,[sal/wages]

如果您不需要重复记录,请使用UNION

答案 1 :(得分:1)

使用union all

select id, sal as [sal/Wages] from table1
union all
select id, wages as [sal/Wages] from table2
order by 1

请注意,我使用了union all而不是union,因为union会从结果集中删除重复项。有时它可能会有用,但我认为不是这样。