问题说明和数据库条件:数据库中有 3 个表(Highschooler、Friend和Likes),这是数据库:
Highschooler
ID name grade
1510 Jordan 9
1689 Gabriel 9
1381 Tiffany 9
1709 Cassandra 9
1101 Haley 10
1782 Andrew 10
1468 Kris 10
1641 Brittany 10
1247 Alexis 11
1316 Austin 11
1911 Gabriel 11
1501 Jessica 11
1304 Jordan 12
1025 John 12
1934 Kyle 12
1661 Logan 12
Likes
ID1 ID2
1689 1709
1709 1689
1782 1709
1911 1247
1247 1468
1641 1468
1316 1304
1501 1934
1934 1501
1025 1101
问题是:对于每对都喜欢对方的学生,返回两个学生的姓名和年级。每对仅包含一次,两个名称按字母顺序排列。
我的查询是:
select H1.name,H1.grade,H2.name,H2.grade from
(select ID1,ID2 from Likes
where ID1 in (select ID2 from Likes) and
ID2 in (select ID1 from Likes)) as G JOIN Highschooler H1 JOIN Highschooler H2
where H1.ID=G.ID1 and H2.ID=G.ID2 order by H1.name,H2.name
我的查询输出是:
Cassandra 9 Gabriel 9
Gabriel 9 Cassandra 9
Jessica 11 Kyle 12
Kyle 12 Jessica 11
预期输出为:
Cassandra 9 Gabriel 9
Jessica 11 Kyle 12
我无法移除一对。我怎样才能在 SQlite 中做到这一点?