SQL Server 2012检索表的2列的属性

时间:2015-04-11 01:48:03

标签: sql sql-server join sql-server-2012

我有以下表格。

Highschooler

enter image description here

enter image description here

ID1和ID2是Highschooler的ID列中的ID(它们不相同)。我如何为这两列选择等级,所以我有类似的东西:

ID1   Grade    ID2   Grade

1 个答案:

答案 0 :(得分:0)

您应join Friends Highschooler两次{对于每个ID

select t1.ID1
     , t2.grade
     , t1.ID2
     , t3.grade
from Friends t1 
join Highschooler t2 on t1.ID1 = t2.ID 
join Highschooler t3 on t1.ID2 = t3.ID