简体   繁体   中英

SQL Server 2012 Retrieving Attributes for 2 Columns of a Table

I have the following tables.

Highschooler

在此处输入图片说明

Friends

在此处输入图片说明

And ID1 and ID2 are IDs (they are not the same) in the ID column of Highschooler. How can I select grades for both of these columns so I have something like:

ID1   Grade    ID2   Grade

You should join Friends with Highschooler two times (for each ID column)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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