2013-02-09 56 views
4

我在做一個免費的斯坦福大學在線課程(這很酷,你應該檢查一下),我一直在絞盡腦汁,天,無法找到以下問題的答案。請幫忙。SQL - 查找僅喜歡同一年級學生的學生的成績

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

在此先感謝您。

問候。

塞薩爾

+1

這是一個很棒的課程,去年我做到了。他們沒有論壇可以提問嗎? – Andomar 2013-02-09 12:02:32

+0

是的,他們這樣做。但我在那裏找不到任何幫助。我想很多學生在這個問題上很難,或者他們根本無法分享這些信息。 – 2013-02-09 12:09:55

回答

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 

編輯:如果你還要求他們至少有一個朋友,你需要檢查太

+0

OMG男人。你是個天才。你一眼就解決了它。 我希望我擅長SQL。 ;) 非常感謝。這正是我所期待的。 – 2013-02-09 12:07:56

+1

@Cesar Zapata困難的部分是圍繞你可以使用子查詢和聯接類型的所有方式,然後你很好去 - 至少直到你需要做臨時表或遊標,樞軸或連接或什麼東西:P – Patashu 2013-02-09 12:09:49

+0

對於我來說,使用子查詢和所有不同類型的操作符仍然非常困難,而且我一直想用一種編程邏輯的方式來思考問題,在這種方式中,您只需將代碼分解並向下移動一步即可一段時間,分開的值,將它們保存在變量中,並逐步處理代碼。我仍然很難理解SQL將如何比較元組或列和所有......它仍然非常混亂。但我會到達那裏。 非常感謝你的時間和幫助。 來自巴西的問候。 塞薩爾。 – 2013-02-09 12:17:20

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 < >爲H2.grade)。然後,外部查詢只列出了這個內部關係中沒有特徵的所有學生。

相關問題