mysql无法将列从查询传递到嵌套子查询

时间:2018-05-12 10:27:47

标签: mysql

我想要的是找到与user2相关联的每个朋友(id = 2),然后找到每个user2朋友和user1之间的共同朋友的数量。

function whereIsAlice(names) { for (var i = 0; i < names.length; i++) { if (names[i].firstName == "Alice") { return i; } } // When not found in above loop then return -1, not found! return -1; } var friends1 = [ { firstName: 'John', lastName: 'Gaudet' }, { firstName: 'Lisa', lastName: 'Mcclanahan' }, { firstName: 'Alice', lastName: 'Vore' }, // Alice is here, at index 2 { firstName: 'Marine', lastName: 'Salsbury' } ]; var friends2 = [ { firstName: 'Tim' }, { firstName: 'Arthur' }, { firstName: 'Juan' } ]; console.log(whereIsAlice(friends1)); //Should be 2 console.log(whereIsAlice(friends2)); // Should be -1

表用户

mysql to find user2 Friends and get mutual_count of Friends with user1 ( with every user2 friend)

表朋友

user_id |   username
------------------
1       |   user1
2       |   user2
3       |   user3
4       |   user4
5       |   user5
6       |   user6
7       |   user7

预期输出:用户2的朋友//(朋友姓名,朋友ID,当前朋友ID和用户1之间的朋友相互计数(id = 1))

user_one_id |   user_two_id
------------------------
1           |   4
1           |   5
1           |   6
2           |   3
2           |   4
3           |   1
3           |   4
5           |   2
5           |   3
5           |   4
6           |   2
6           |   3
7           |   2

mysql语句

username | user_id  | mutual_count
------------------------
user3   |  3        | 3     // user3 and user1 mutual ->  (user4,user5,user6)
user4   |  4        | 2     // user4 and user1 mutual ->  (user3 ,user5)
user5   |  5        | 2     // user5 and user1 mutual ->  (user3 ,user4)
user6   |  6        | 1     // user6 and user1 mutual ->  ( user3)
user7   |  7        | 0    // user7 and user1 mutual ->   (0)

我得到的错误 未知栏&#39; users.user_id&#39;在&#39; where子句&#39; 获取mutual_count canot的问题子查询知道users.user_id

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

我修改了你获得所需输出的尝试(但可能有更好的方法来获得你想要的东西)。

似乎主要问题是所有子查询都无法访问顶部外部查询中的users表。所以我修改了需要该表的子查询,以便它可以通过其直接外部查询中的users子句访问WHERE表。

SELECT  users.username,users.user_id,

    (SELECT count(a.friendID) FROM
        (   SELECT user_two_id friendID, user_one_id where_id FROM friends
            UNION 
            SELECT user_one_id friendID, user_two_id where_id FROM friends
        ) AS a 
        JOIN  
        (   SELECT user_two_id friendID FROM friends WHERE user_one_id = 1
            UNION 
            SELECT user_one_id friendID FROM friends WHERE user_two_id = 1
        ) AS b 
        ON  a.friendID = b.friendID
        WHERE a.where_id = users.user_id
    ) as mutual_count

FROM friends LEFT JOIN  users    
ON friends.user_one_id = users.user_id or  friends.user_two_id = users.user_id  
WHERE (friends.user_one_id = 2 OR friends.user_two_id = 2)   AND users.user_id !=2
order by user_id

<强>输出:

+----------+---------+--------------+
| username | user_id | mutual_count |
+----------+---------+--------------+
|   user3  |    3    |       3      |
+----------+---------+--------------+
|   user4  |    4    |       2      |
+----------+---------+--------------+
|   user5  |    5    |       2      |
+----------+---------+--------------+
|   user6  |    6    |       1      |
+----------+---------+--------------+
|   user7  |    7    |       0      |
+----------+---------+--------------+

修改后的行:

第4行: SELECT user_two_id friendID, user_one_id where_id FROM friends

第6行: SELECT user_one_id friendID, user_two_id where_id FROM friends

第14行: WHERE a.where_id = users.user_id