显示两个学生在sql中彼此喜欢的学生列表

时间:2014-02-20 02:47:24

标签: sql sqlite

我有一个示例数据库,您可以在此处查看:https://class.stanford.edu/c4x/Engineering/db/asset/socialdata.html

我正在尝试编写一个执行以下操作的sql语句:显示两个学生彼此喜欢的学生列表。

这是我的代码:

SELECT A.name, B.name, C.name, D.name
FROM Highschooler A, Highschooler B, Highschooler C, Highschooler D, Likes L
WHERE A.ID = L.ID1 AND B.ID = L.ID2 AND C.ID = L.ID2 AND D.ID = L.ID1 AND A.name < B.name AND D.name < C.name
ORDER BY A.name, B.name, C.name, D.name ASC;

我不确定它是否正确显示这是我得到的:

Alexis      Kris        Kris        Alexis
Andrew      Cassandra   Cassandra   Andrew
Austin      Jordan      Jordan      Austin
Brittany    Kris        Kris        Brittany
Cassandra   Gabriel     Gabriel     Cassandra
Jessica     Kyle        Kyle        Jessica

1 个答案:

答案 0 :(得分:0)

我想说你要找的是:

select A.name || ' likes ' || B.name
from   Highschooler A
join   likes          on A.id = Likes.ID1
join   Highschooler B on B.id = Likes.ID2