SQL Server:来自两个或多个表的值

时间:2015-01-31 12:51:10

标签: sql sql-server-2008

情况如下:在我的SQL Server 2008数据库中,有一些表:

表1

UID UserName HostName ServerID
-------------------------------
1    Sudheer  PC1       01
2    SAM      PC2       03

表2

ServerID   ServerName
----------------------
1           SRV1-DB1
2           SRV2-WEB
3           SRV3-Mail

我需要输出SELECT

UserName HostName  ServerName
-----------------------------    
Sudheer  PC1        SRV1-DB1
SAM      PC2        SRV3-Mail

1 个答案:

答案 0 :(得分:1)

使用INNER JOIN。请参阅this以了解SQL SERVER中的JOINS

SELECT 
    A.UserName, A.HostName, B.ServerName 
FROM 
    Table1 AS A 
INNER JOIN 
    Table2 AS B ON A.ServerID   = B.ServerID