-1

我的任务是:查找只有同年级朋友的学生的姓名和年级。

我的桌子:

高中生

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

朋友

ID1 ID2
1510    1381
1510    1689
1689    1709
1381    1247
1709    1247
1689    1782
1782    1468
1782    1316
1782    1304
1468    1101
1468    1641
1101    1641
1247    1911
1247    1501
1911    1501
1501    1934
1316    1934
1934    1304
1304    1661
1661    1025
1381    1510
1689    1510
1709    1689
1247    1381
1247    1709
1782    1689
1468    1782
1316    1782
1304    1782
1101    1468
1641    1468
1641    1101
1911    1247
1501    1247
1501    1911
1934    1501
1934    1316
1304    1934
1661    1304
1025    1661

我的查询:

select name, grade from highschooler
where id in
(  select distinct id1 
   from friend out
   where not exists
     (select id1
      from friend
      where    
          (select grade from highschooler where id = out.id1) <>
          (select grade from highschooler where id = out.id2)
     )
)

我的结果:

Jordan  9
Gabriel 9
Tiffany 9
Cassandra   9
Haley   10
Andrew  10
Kris    10
Brittany    10
Alexis  11
Gabriel 11
Jessica 11
Jordan  12
John    12
Kyle    12
Logan   12

预期的查询结果:

Jordan  9
Brittany    10
Haley   10
Kris    10
Gabriel 11
John    12
Logan   12

我用 Tiffany 仔细检查了我的代码:

1381    Tiffany 9   1247    Alexis  11
1381    Tiffany 9   1510    Jordan  9

我的相关子查询有问题。你能告诉我什么是错的吗?

4

4 回答 4

0

要仅获取在同年级有朋友但没有其他朋友的学生,请尝试以下操作:

SELECT DISTINCT H.Id, H.Name, H.Grade
FROM HighSchooler H
  JOIN Friend F ON H.Id = F.ID1
  JOIN HighSchooler H2 ON F.ID2 = H2.ID AND H2.Grade = H.Grade
WHERE NOT EXISTS 
 (SELECT *
  FROM HighSchooler H3
  JOIN Friend F2 ON H3.Id = F2.Id1
  JOIN HighSchooler H4 ON F2.Id2 = H4.Id
  WHERE H4.Grade <> H.Grade AND H3.Id = H.Id)

这是SQL Fiddle

祝你好运。

于 2013-02-03T04:58:53.600 回答
0

我会使用 TDQD(测试驱动的查询设计)来解决这个问题。

确定特定高中生的朋友的成绩

SELECT f.id1 AS h1_id, h1.grade AS h1_grade, h2.grade AS h2_grade
  FROM Friends AS f
  JOIN Highschoolers AS h1 ON f.id1 = h1.id
  JOIN Highschoolers AS h2 ON f.id2 = h2.id

这为每个朋友列出了其朋友的高中生、高中生的年级和朋友的年级。

生成有朋友不在自己年级的学生列表

SELECT DISTINCT h1_id
  FROM (SELECT f.id1 AS h1_id, h1.grade AS h1_grade, h2.grade AS h2_grade
          FROM Friends AS f
          JOIN Highschoolers AS h1 ON f.id1 = h1.id
          JOIN Highschoolers AS h2 ON f.id2 = h2.id
       )
 WHERE h1_grade != h2_grade;

生成至少有一个朋友且没有朋友不在同年级的学生列表

SELECT h.id, h.name, h.grade
  FROM Highschooolers AS h
  JOIN Friends AS f ON h.id = f.id1
 WHERE h.id NOT IN
       (SELECT DISTINCT h1_id
          FROM (SELECT f.id1 AS h1_id, h1.grade AS h1_grade, h2.grade AS h2_grade
                  FROM Friends AS f
                  JOIN Highschoolers AS h1 ON f.id1 = h1.id
                  JOIN Highschoolers AS h2 ON f.id2 = h2.id
               )
         WHERE h1_grade != h2_grade
       )

这将选择确实出现在id1“朋友”表列中的高中生;他们至少列出了一位朋友,而且他们的每一位朋友都在同一年级。

生成没有朋友不在同年级的学生列表

SELECT h.id, h.name, h.grade
  FROM Highschooolers AS h
 WHERE h.id NOT IN
       (SELECT DISTINCT h1_id
          FROM (SELECT f.id1 AS h1_id, h1.grade AS h1_grade, h2.grade AS h2_grade
                  FROM Friends AS f
                  JOIN Highschoolers AS h1 ON f.id1 = h1.id
                  JOIN Highschoolers AS h2 ON f.id2 = h2.id
               )
         WHERE h1_grade != h2_grade
       )

这将选择未出现在id1Friends 表列中的高中生;他们没有列出朋友(可能没有输入数据,而不是非常孤独),所以他们的朋友中没有一个是不同年级的。

请注意,所有这些查询都假定学生 B 不一定将学生 A 列为朋友,因为学生 A 将学生 B 列为朋友。也就是说,如果条目 (1234, 2345) 出现在朋友表中(说明学生 1234 认为 2345 是朋友),则不会自动得出 2345 认为 1234 是朋友;必须有一个明确的逆向条目(2345、1234)来指示互惠关系。如果友谊是自动互惠的,那么查询会变得更加复杂。

于 2013-02-03T05:34:57.957 回答
0

这有效:

select "name", "grade" from highschooler a
where not exists
(
  select 1 from (
(select id1, id2, count(distinct "grade") from (
SELECT *
FROM friend f, highschooler h
where f.id1 = h.id
or f.id2 = h.id
order by id1,id2)
group by id1, id2
having count(distinct "grade") > 1))
  where id1 = a.id or id2 = a.id)
order by "grade", "name"

上述查询的 SQLFIDDLE

于 2013-02-03T05:35:42.107 回答
0

这肯定有效,我试过运行它......

Select distinct h1.name,h1.grade  
From Highschooler h1,Highschooler h2,Friend
Where h1.ID = Friend.ID1
AND h2.ID = Friend.ID2
AND h1.grade = h2.grade

EXCEPT
Select distinct h1.name,h1.grade
From Highschooler h1,Highschooler h2,Friend
Where h1.ID = Friend.ID1
AND h2.ID = Friend.ID2
AND h1.grade <> h2.grade
order by h1.grade,h1.name
于 2013-02-04T12:44:01.173 回答