我有这个查询,它给出了初始结果。我的最终目标是解决以下问题:
对于彼此喜欢的每对学生,请返回两名学生的姓名和成绩
这是我写的初始查询,以及匹配的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将在此表中:
学生(身份证,姓名,职系)
那么,我该如何做到以下几点:
预期查询结果:
name1 grade1 name2 grade2
name1 grade1 name2 grade2
名称和成绩来自学生表,基于第一个查询的ID?
答案 0 :(得分:0)
Select L1.name as name1, L1.grade as grade1, L2.name as name2, L2.grade as gade2 from ...
答案 1 :(得分: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