SQLite查询问题

时间:2013-01-28 06:53:40

标签: sql database

问题规范和数据库条件:数据库中有3个表( Highschooler 朋友喜欢),这是数据库:< / p>

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行额外的错误。谁能指出我在这里做错了什么?

5 个答案:

答案 0 :(得分: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,而是选择一个虚拟表,并直接在其上放置需求。

答案 1 :(得分: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

答案 2 :(得分: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

编辑:更正了答案(我认为'喜欢'是使用'朋友'表表达的)。也不明白'喜欢'(我不是用FB ;-))

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

答案 3 :(得分: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)

应该让你关闭

答案 4 :(得分:0)

像这样的东西(sytax可能错了)并用正确的列替换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