SQL查询我遇到了麻烦

时间:2013-02-16 21:19:27

标签: sql database sqlite

我有这些表格:

Highschooler(身份证,姓名,职系) 有一名高中学生,他有一个独特的身份证和一定等级的名字。

朋友(ID1,ID2) ID1的学生是ID2学生的朋友。友谊是相互的,所以如果(123,456)在朋友表中,那么(456,123)。

喜欢(ID1,ID2) ID1的学生喜欢ID2的学生。喜欢某人不一定是相互的,所以如果(123,456)在Likes表中,则不能保证(456,123)也存在。

如果两个学生A和B是朋友,而A喜欢B但反之亦然,我想删除Likes元组。

首先,我尝试只获得2个非互惠喜欢的朋友,但我没有成功......

我有这个:

SELECT * 
FROM Likes
where Likes.ID1 in (
   select Likes.ID1 
   from Likes, Friend
   where Likes.ID1=Friend.ID1 
     and Likes.ID2=Friend.ID2
   ) 
AND NOT IN (
   SELECT Likes.ID1 
   FROM Likes L1, Likes L2, Friend
   WHERE L1.ID1=Friend.ID1 
     AND L2.ID2=Friend.ID2 
     AND L1.ID1 = L2.ID2 
     AND L1.ID2 = L2.ID1);

但它不起作用..我收到错误..有人可以帮我这个!

我实际上有一张带有值的表格图片,但我无法发布,因为我需要超过10个声誉才能做到这一点......

编辑:

好的,我要把桌子放在这里因为我还没有得到正确的结果

Highschooler
ID  name    grade
1510    Jordan  9
1689    Gabriel 9
1381    Tiffany 9
1709    Cassand 9
1101    Haley   10
1782    Andrew  10
1468    Kris    10
1641    Brit    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

我应该从最喜欢的表中消除这两个元组: 1911年至1247年 1641至1468年

他们是非互惠的喜欢谁也是朋友

4 个答案:

答案 0 :(得分:1)

由于您正在为此问题进行Stanford Intro to SQL课程,因此我将提供我的工作答案。提及它必须在SQlite中工作是一个好主意。我是SQL的初学者,我非常肯定有一个更优雅的解决方案,但这应该通过自动化测试。这是我的解决方案:

DELETE FROM likes
WHERE likes.id1 in (Select s1.id
FROM highschooler s1, highschooler s2, friend, likes
WHERE s1.id <> s2.id
  AND friend.id1 = s1.id 
  AND friend.id2 = s2.id
  AND likes.id1 = s1.id 
  AND likes.id2 = s2.id
  AND s2.id NOT IN (SELECT s3.id FROM likes, highschooler s3, highschooler s4
                    WHERE s3.id = s2.id 
                        AND s4.id = s1.id
                        AND s3.id <> s4.id 
                        AND likes.id1 = s3.id 
                        AND likes.id2 = s4.id))

答案 1 :(得分:0)

试试这个

  SELECT * 
 FROM Likes
 where Likes.ID1 in 
               (select Likes.ID1 from Likes, Friend where Likes.ID1=Friend.ID1 and Likes.ID2=Friend.ID2) 
  AND Likes.ID1 NOT IN (SELECT Likes.ID1 FROM Likes L1, Likes L2, Friend WHERE L1.ID1=Friend.ID1 AND L2.ID2=Friend.ID2 AND L1.ID1 = L2.ID2 AND L1.ID2 = L2.ID1);

答案 2 :(得分:0)

您的错误是因为您需要在第二个AND条件中重复Likes.ID1字段。

SELECT * 
FROM Likes
where Likes.ID1 in (select Likes.ID1
                    from Likes, Friend
                    where Likes.ID1=Friend.ID1 and Likes.ID2=Friend.ID2)
  AND Likes.ID1 NOT IN (SELECT Likes.ID1
             FROM Likes L1, Likes L2, Friend
             WHERE L1.ID1=Friend.ID1
               AND L2.ID2=Friend.ID2
               AND L1.ID1 = L2.ID2
               AND L1.ID2 = L2.ID1);

我没有分析您的查询是否会返回正确的结果。这可能是,

select *
from Likes L1
  inner join Friend on L1.ID1 = Friend.ID1
  left outer join Likes L2 on Friend.ID2 = L2.ID1 and L1.ID1 = L2.ID2
where L2.ID2 is null

答案 3 :(得分:0)

虽然这并不是在查询中找到错误,但我会这样做:

SELECT * 
FROM Likes L1
WHERE
EXISTS ( -- friendship exists
   SELECT *
   FROM Friends F 
   WHERE  F.ID1 IN (L1.ID1, L1.ID2) AND F.ID2 IN (L1.ID1, L1.ID2)
)
AND NOT EXISTS ( -- reverse like does not exist
   SELECT *
   FROM Likes L2
   WHERE L1.ID1  = L2.ID2 AND L1.ID2 =  L2.ID1
);

请注意,AB中的朋友LikesFriends的顺序可能不同,因此我们需要JOIN使用IN像我一样,或OR声明。