0

我有这个查询,它给出了显示的初始结果。我的最终目标是解决以下问题:

对于每对都喜欢对方的学生,返回两个学生的姓名和年级

这是我编写的初始查询,以及匹配的 ID 对:

select L1.ID1, L1. ID2
from Likes L1, Likes L2
where L1.ID1 = L2.ID2 and L2.ID1 = L1.ID2 and L1.ID1 > L1.ID2

我的查询结果:

1709 1689
1934 1501

我遇到的问题是返回每个 ID 的名称和等级,其中 ID 将在此表中:

学生(ID、姓名、年级)

那么,我该如何获得以下信息:

预期的查询结果:

name1 grade1 name2 grade2
name1 grade1 name2 grade2

根据第一个查询的 ID,姓名和成绩来自学生表的哪里?

4

2 回答 2

0

两个加入学生应该做的伎俩

SELECT 
l1.ID1, l1.ID2
s1.name as name1, s1.grade as grade1, 
s2.name as name2, s2.grade as grade2
FROM Likes l1
INNER JOIN Likes l2 ON l1.ID1 = l2.ID2 and l2.ID1 = l1.ID2
INNER JOIN students s1 on s1.ID = l1.ID1
INNER JOIN students s2 on s2.ID = l1.ID2
WHERE l1.ID1 > l1.ID2
于 2013-01-28T08:24:29.447 回答
0
Select L1.name as name1, L1.grade as grade1, L2.name as name2, L2.grade as gade2 from ...
于 2013-01-28T05:44:27.650 回答