简体   繁体   English

为什么我的SQL查询不起作用?

[英]Why doesn't my SQL query work?

I am working on the following problem: 我正在解决以下问题:

For each student A who likes a student B where the two are not friends, find if they have a friend C in common (who can introduce them!). 对于每个喜欢两个都不是朋友的学生B的学生A,找出他们是否有一个共同的朋友C(谁可以介绍他们!)。 For all such trios, return the name and grade of A, B, and C. 对于所有这些三重奏,返回名称A,B和C。

Highschooler ( ID, name, grade ) 
English: There is a high school student with unique ID and a given first name in a certain grade. 

Friend ( ID1, ID2 ) 
English: The student with ID1 is friends with the student with ID2. Friendship is mutual, so if (123, 456) is in the Friend table, so is (456, 123). 

Likes ( ID1, ID2 ) 
English: The student with ID1 likes the student with ID2. Liking someone is not necessarily mutual, so if (123, 456) is in the Likes table, there is no guarantee that (456, 123) is also 

My query is below: 我的查询如下:

SELECT student_a.name, student_a.grade,
       student_b.name, student_b.grade,
       student_c.name, student_c.grade
FROM Highschooler student_a
    INNER JOIN Likes
        ON (Likes.ID1 = student_a.ID)
    INNER JOIN Highschooler student_b
        ON (Likes.ID2 = student_b.ID)
    INNER JOIN Friend friend_1
        ON (friend_1.ID1 = student_a.ID)
    INNER JOIN Highschooler student_C
        ON (student_c.ID = friend_1.ID2)
    INNER JOIN Friend friend_2
        ON (student_c.ID = friend_2.ID1
            AND student_b.ID = friend_2.ID2)
WHERE student_b.ID <> friend_1.ID2

I get the following: 我得到以下内容:

Andrew 10 Cassandra 9 Gabriel 9 Brittany 10 Kris 10 Haley 10 Austin 11 Jordan 12 Andrew 10 Austin 11 Jordan 12 Kyle 12 Gabriel 11 Alexis 11 Jessica 11

When the actual output should be: 何时实际输出应为:

Andrew 10 Cassandra 9 Gabriel 9 Austin 11 Jordan 12 Andrew 10 Austin 11 Jordan 12 Kyle 12

I reviewed my query a ton of times, yet can't point out the edge cases that are messing me up. 我检查了我的查询很多次,但无法指出困扰我的极端情况。 I know this sort of an ill-posed question. 我知道这是个不适的问题。 However, I can't seem to pinpoint my error. 但是,我似乎无法指出我的错误。

Let's see the query: Since "like" is a unidirectional relation, it is accurate to see only a single direction, but since friends is symmetrical, you need to be clever. 让我们看一下查询:由于“喜欢”是单向关系,因此仅查看单个方向是准确的,但是由于朋友是对称的,因此您需要变得聪明。 You are having on clauses in your query, where you handle the friends relation as it was assymetric as well. 您在查询中有on子句,您在其中也处理了不对称的friends关系。 You assume that student_a.ID = friend_1.ID1 and student_c.ID = friend_1.ID2 if a and c are friends. 如果ac是朋友,则假定student_a.ID = friend_1.ID1student_c.ID = friend_1.ID2朋友student_c.ID = friend_1.ID2 Why do you consider the other direction impossible? 您为什么认为另一个方向不可能? Similarly, you assume that friend_2.ID1 = student_c.ID and friend_2.ID = student_b.ID . 同样,您假设friend_2.ID1 = student_c.ID并且friend_2.ID = student_b.ID Why is the other direction impossible? 为什么另一个方向不可能?

At the end you have this: 最后你有这个:

WHERE student_b.ID <> friend_1.ID2

Where you exclude the case that student_b.ID is the second friend in the first friendship, but you do not exclude that b is the first friend or that a participates in the second friendship. 如果您排除了student_b.ID是第一个朋友中的第二个朋友的情况,但不排除b是第一个朋友或a参加了第二个朋友的情况。

Suggested solution: 建议的解决方案:

SELECT student_a.name, student_a.grade,
       student_b.name, student_b.grade,
       student_c.name, student_c.grade
FROM Highschooler student_a
    INNER JOIN Likes
        ON (Likes.ID1 = student_a.ID)
    INNER JOIN Highschooler student_b
        ON (Likes.ID2 = student_b.ID)
    INNER JOIN Friend friend_1
        ON (friend_1.ID1 = student_a.ID) OR (friend_1.ID2 = student_a.ID)
    INNER JOIN Highschooler student_C
        ON (student_c.ID = friend_1.ID1) OR (student_c.ID = friend_1.ID2)
    INNER JOIN Friend friend_2
        ON ((student_c.ID = friend_2.ID1 OR student_c.ID = friend_2.ID2)
            AND 
            (student_b.ID = friend_2.ID1 OR student_b.ID = friend_2.ID))

Note, that the query above was not tested and that a where is needed to make sure that a is different from b is different from c is different from a if a student can like himself or be friends with himself. 请注意,上面的查询没有测试,一个where ,以确保需要a不同于b不同于c是从不同的a ,如果学生能像自己或者是朋友与他自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM