繁体   English   中英

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

[英]Getting Different column values from Multiple tables in 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语句来做到这一点?

使用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

全部使用工会

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会从结果集中删除重复项。 有时它可能有用,但我认为对您而言却不是。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM