通过外键从另一个表中获取值

时间:2016-01-26 18:40:09

标签: sql sql-server

我的SQL Server数据库中有两个表:

Person

ID Lastname  Age
1  Jones     46
2  Smith     37

Employee

ID(FK) Firstname Surname
1      Bob       [empty string]
2      Janet     [empty string]

我想将Employee组合如下。 (我将删除姓氏,但现在不要担心。)

Employee 
ID (FK)   Firstname  Surname
1         Bob        Jones
2         Janet      Smith

我该怎么做?

2 个答案:

答案 0 :(得分:2)

我认为您应该尝试此查询,通过这种方式,您将获得基于两个表的所需结果。

Select E.ID, E.FirstName P.Lastname As Surname From Person P
Inner join Employee E ON p.ID = E.ID

答案 1 :(得分:2)

Select E.ID, E.Firstname, P.Lastname as Surname from Employee E 
inner join Person P on P.ID=E.ID