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

时间:2016-03-05 23:01:48

标签: mysql

我正在解决以下问题:

对于每个学生A喜欢学生B,其中两个不是朋友,找出他们是否有共同的朋友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 

我的查询如下:

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

我得到以下内容:

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

当实际输出应为:

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

我多次查看了我的查询,但无法指出让我搞砸的边缘情况。我知道这是一个不合时宜的问题。但是,我似乎无法确定我的错误。

1 个答案:

答案 0 :(得分:0)

让我们看看查询:因为&#34;喜欢&#34;是一个单向的关系,只看到一个方向是准确的,但由于朋友是对称的,你需要聪明。您在查询中有on个子句,您可以在其中处理朋友关系,因为它也是不对称的。如果student_a.ID = friend_1.ID1student_c.ID = friend_1.ID2是朋友,则您认为ac。为什么你认为其他方向不可能?同样,您假设friend_2.ID1 = student_c.IDfriend_2.ID = student_b.ID。为什么另一个方向不可能?

最后你有这个:

WHERE student_b.ID <> friend_1.ID2

如果您排除student_b.ID是第一个友谊中的第二个朋友的情况,但您不排除b是第一个朋友或a参与第二个友谊。

建议的解决方案:

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))

请注意,上述查询未经过测试,确保wherea不同bc不同,需要a var userChoice = prompt("Do you choose Rock, Paper, Scissors, Lizard or Spock?"); var computerChoice = Math.random(); if (computerChoice < 0.21) { computerChoice = "Rock"; } else if(computerChoice > 0.20 && computerChoice < 0.41) { computerChoice = "Paper"; } else if(computerChoice >0.40 && computerChoice < 0.61) { computerChoice = "Scissors"; } else if(computerChoice > 0.60 && computerChoice < 0.81) { computerChoice = "Lizard"; } else { computerChoice = "Spock"; } console.log("The player chooses: " + userChoice); console.log("The computer chooses: " + computerChoice); function compare(choice1, choice2) { if(choice1 === choice2) { console.log("The result is a tie!"); } else if(choice1 === "Rock") { if(choice2 === "Scissors" || "Lizard") { console.log("Rock wins!"); } else if(choice2 === "Paper") { console.log("Paper wins!"); } else { console.log("Spock wins"); } } else if(choice1 === "Paper") { if(choice2 === "Rock" || "Spock") { console.log("Paper wins!"); } else if(choice2 === "Scissors") { console.log("Scissors wins!"); } else { console.log("Lizard wins!"); } } else if(choice1 === "Scissors") { if(choice2 === "Paper" || "Lizard") { console.log("Scissors wins!"); } else if(choice2 === "Rock") { console.log("Rock wins!"); } else { console.log("Spock wins!"); } } else if(choice1 === "Lizard") { if(choice2 === "Paper" || "Spock") { console.log("Lizard wins!"); } else if(choice2 === "Rock") { console.log("Rock wins!"); } else { console.log("Spock wins!"); } } else if(choice1 === "Spock") { if(choice2 === "Rock" || "Scissors") { console.log("Spock wins!"); } else if(choice2 === "Paper") { console.log("Paper wins!"); } else { console.log("Lizard wins!"); } } } compare(userChoice, computerChoice); 如果学生可以喜欢自己或与自己成为朋友。