-1

问题说明和数据库条件:数据库中有 3 个表(HighschoolerFriendLikes),这里是数据库:

Highschooler
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

Friend
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

Likes
ID1 ID2
1689    1709
1709    1689
1782    1709
1911    1247
1247    1468
1641    1468
1316    1304
1501    1934
1934    1501
1025    1101

问题是:For every student who likes someone 2 or more grades younger than themselves, return that student's name and grade, and the name and grade of the student they like.

我的查询是:

select HS11.name,HS11.grade,HS22.name,HS22.grade from
( select ID1,ID2 from Likes where ID1 in
(
select HS1.ID from Highschooler HS1, Highschooler HS2 
where HS1.ID<> HS2.ID and (HS1.grade - HS2.grade) >=2 
)
and ID2 in
(
select HS2.ID from Highschooler HS1, Highschooler HS2 
where HS1.ID<> HS2.ID and (HS1.grade - HS2.grade) >=2 
)
) as G, Highschooler HS11, Highschooler HS22
where HS11.ID=G.ID1 and HS22.ID = G.ID2

我的查询输出是:

Alexis 11 Kris 10
John 12 Haley 10

预期输出为:

John 12 Haley 10

所以,我得到了额外的 1 行,这是不正确的。谁能指出我在这里做错了什么?

4

5 回答 5

1

您的SELECT陈述可以通过使用来简化INNER JOIN。然后你可以有一个WHERE条款来指定要求:

SELECT HS1.name, HS1.grade, HS2.name, HS2.grade
FROM Likes
INNER JOIN Highschooler HS1
  ON Likes.ID1 = HS1.ID
INNER JOIN Highschooler HS2
  ON Likes.ID2 = HS2.ID
WHERE (HS1.grade - HS2.grade) >=2 

换句话说,不是试图通过使用具有复杂需求的子查询来找到所需的 ID,而是选择一个虚拟表,然后直接在其上放置需求。

于 2013-01-28T07:04:56.230 回答
1

我会去

select a.name as liker, a.grade as liker_grade, c.name as likee, c.grade as likee_grade from
highschooler a inner join likes b on a.id = b.id1
left join highschooler c on b.id2 = c.id
where a.grade >= c.grade +2

如果喜欢总是相互的(通常情况并非如此,请与自身结合并联合所有

select a.name as liker, a.grade as liker_grade, c.name as likee, c.grade as likee_grade from
highschooler a inner join likes b on a.id = b.id1
left join highschooler c on b.id2 = c.id
where a.grade >= c.grade +2
union all
select a.name as liker, a.grade as liker_grade, c.name as likee, c.grade as likee_grade from
highschooler a inner join likes b on a.id = b.id2
left join highschooler c on b.id1 = c.id
where a.grade >= c.grade +2
于 2013-01-28T07:19:54.013 回答
0

如果是,那么这应该有效

Select main.*
  from Highscooler main
       join Likes lk on main.ID = lk.ID1
       join Highscooler mate on mate.ID = lk.ID2
  where main.Grade >= mate.Grade+2

编辑:更正了答案(我认为“喜欢”是使用“朋友”表表达的)。也不懂'like'(我没有使用FB ;-))

再次更正:我不擅长阅读文字......

于 2013-01-28T07:01:19.257 回答
0
SELECT   liker.ID AS LikerID,
         liker.Name AS LikerName,
         liker.Grade AS LikerGrade,
         liked.ID AS LikedID,
         liked.Name AS LikedName,
         liked.Grade AS LikedGrade
FROM     Likes
JOIN     HighSchooler Liker ON Likes.ID1 = Liker.ID
JOIN     HighSchooler Liked ON Likes.ID2 = Liked.ID
WHERE    2 <= (Liker.Grade - Liked.Grade)

应该让你靠近

于 2013-01-28T07:02:57.950 回答
0

像这样的东西(语法可能是错误的)并select *用正确的列替换

select * from highschooler as me
left join on friend where me.id = friend.id1
left join on highschooler as f where f.id = freind.id2
where me.grade - f.grade >= 2
于 2013-01-28T07:05:04.787 回答