SQL - 查找仅喜欢同一年级学生的学生成绩

时间:2013-02-09 11:58:12

标签: sql find any

我是一个免费的斯坦福在线课程(非常酷,你应该检查出来),我已经绞尽脑汁待了2天,无法找到以下问题的答案。请帮忙。

问题4 查找只有同一年级朋友的学生的姓名和成绩。返回按年级排序的结果,然后按每个年级中的名称返回。

当我终于认为我得到了答案时,我的查询返回了表朋友的所有值。

这是我能想到的最好的。

select h1.id, h1.name, h1.grade, h2.id, h2.name, h2.grade
from friend f1
join highschooler h1 on f1.id1 = h1.id
join highschooler h2 on f1.id2 = h2.id
where h1.grade = any (select h3.grade from friend f2
                    join highschooler h3 on f2.id1 = h3.id
                    where h3.id = f1.id1)

我需要在SQL Lite中运行查询。 我正在使用http://sqlfiddle.com在SQL Lite中测试我的查询,这是我正在使用的示例数据。

/* Create the schema for our tables */
create table Highschooler(ID int, name text, grade int);
create table Friend(ID1 int, ID2 int);
create table Likes(ID1 int, ID2 int);

/* Populate the tables with our data */
insert into Highschooler values (1510, 'Jordan', 9);
insert into Highschooler values (1689, 'Gabriel', 9);
insert into Highschooler values (1381, 'Tiffany', 9);
insert into Highschooler values (1709, 'Cassandra', 9);
insert into Highschooler values (1101, 'Haley', 10);
insert into Highschooler values (1782, 'Andrew', 10);
insert into Highschooler values (1468, 'Kris', 10);
insert into Highschooler values (1641, 'Brittany', 10);
insert into Highschooler values (1247, 'Alexis', 11);
insert into Highschooler values (1316, 'Austin', 11);
insert into Highschooler values (1911, 'Gabriel', 11);
insert into Highschooler values (1501, 'Jessica', 11);
insert into Highschooler values (1304, 'Jordan', 12);
insert into Highschooler values (1025, 'John', 12);
insert into Highschooler values (1934, 'Kyle', 12);
insert into Highschooler values (1661, 'Logan', 12);

insert into Friend values (1510, 1381);
insert into Friend values (1510, 1689);
insert into Friend values (1689, 1709);
insert into Friend values (1381, 1247);
insert into Friend values (1709, 1247);
insert into Friend values (1689, 1782);
insert into Friend values (1782, 1468);
insert into Friend values (1782, 1316);
insert into Friend values (1782, 1304);
insert into Friend values (1468, 1101);
insert into Friend values (1468, 1641);
insert into Friend values (1101, 1641);
insert into Friend values (1247, 1911);
insert into Friend values (1247, 1501);
insert into Friend values (1911, 1501);
insert into Friend values (1501, 1934);
insert into Friend values (1316, 1934);
insert into Friend values (1934, 1304);
insert into Friend values (1304, 1661);
insert into Friend values (1661, 1025);
insert into Friend select ID2, ID1 from Friend;

insert into Likes values(1689, 1709);
insert into Likes values(1709, 1689);
insert into Likes values(1782, 1709);
insert into Likes values(1911, 1247);
insert into Likes values(1247, 1468);
insert into Likes values(1641, 1468);
insert into Likes values(1316, 1304);
insert into Likes values(1501, 1934);
insert into Likes values(1934, 1501);
insert into Likes values(1025, 1101);

提前谢谢。

问候。

塞萨尔

2 个答案:

答案 0 :(得分:4)

所以我们想找到那些没有其他年级学生的学生,他们有友谊关系,对吧?这是表达这一点的一种方式:

select * from highschooler h
where not exists
(select 1 from highschooler h2 where h2.grade != h.grade and exists
(select 1 from friends f where (f.id1 = h.id or f.id2 = h.id) and (f.id1 = h2.id or f.id2 = h2.id)))
order by grade, name

编辑:如果你还要求他们至少有一个朋友,你也需要检查这个

答案 1 :(得分:3)

我的解决方案:

SELECT name, grade
FROM Highschooler
WHERE ID NOT IN
(SELECT ID1
FROM Friend F1 JOIN Highschooler H1
ON H1.ID = F1.ID1
JOIN Highschooler H2
ON H2.ID = F1.ID2
WHERE H1.grade <> H2.grade)
ORDER BY grade, name

基本上,内部子查询返回学生与具有不同成绩的朋友的关系(其中H1.grade&lt;&gt;到H2.grade)。然后外部查询只列出所有没有这种内在关系特征的学生。