【问题标题】:SQL - Find the grade of students who only like students in the same gradeSQL - 查找只喜欢同年级学生的学生的年级
【发布时间】:2013-02-09 11:58:12
【问题描述】:

我正在做一个免费的斯坦福在线课程(这很酷,你应该去看看),最近 2 天我一直在绞尽脑汁,找不到以下问题的答案。请帮忙。

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

当我最终认为我有答案时,我的查询返回了 Friend 表中的所有值。

这是我能想到的最好的了。

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);

提前谢谢你。

问候。

塞萨尔

【问题讨论】:

  • 这是一门很棒的课程,我去年做过。他们没有可以提问的论坛吗?
  • 是的,他们有。但我在那里找不到任何帮助。我想很多学生在这方面遇到了困难,或者他们根本无法分享信息或其他东西。

标签: sql find any


【解决方案1】:

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

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

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

【讨论】:

  • 天啊人。你是个天才。你一眼就解决了。我希望我擅长 SQL。 ;) 非常感谢你。这正是我想要的。
  • @Cesar Zapata 困难的部分是围绕所有可以使用子查询和连接类型的方法,然后你就可以开始了 - 至少在你需要做临时表或光标或枢轴或连接或其他东西:P
  • 对我来说,使用子查询和所有不同类型的运算符仍然很困难,而且我一直在思考如何使用编程逻辑来实现,你只需将代码分解一次移动一步,分离值,将它们保存在变量中并逐步操作代码。我仍然很难理解 SQL 将如何比较元组或列等等......它仍然非常混乱。但我会到达那里。非常感谢您的时间和帮助。来自巴西的问候。塞萨尔。
【解决方案2】:

我的解决方案:

选择姓名、年级 从高中生 ID 不在的地方 (选择 ID1 来自朋友 F1 加入高中生 H1 开 H1.ID = F1.ID1 加入高中生H2 开 H2.ID = F1.ID2 WHERE H1.grade H2.grade) 按年级、姓名排序

本质上,内部子查询返回学生与具有不同成绩的朋友的关系(其中 H1.grade 到 H2.grade)。然后外部查询简单地列出了所有不在这个内部关系中的学生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多