gpt4 book ai didi

php - 在关注者和关注者关系表上获取共同好友

转载 作者:行者123 更新时间:2023-11-29 18:11:26 25 4
gpt4 key购买 nike

如何获得共同好友的 SQL 查询?

我的SQL

但我找不到共同的 friend

enter image description here

id | user_id
--------------
1 | 2
2 | 3
3 | 4
4 | 1

Relation Table

id | follower_id | following_id
--------------------------------
1 | 1 | 2
2 | 2 | 1
3 | 1 | 4
4 | 4 | 3
5 | 4 | 1

以下查询

"SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `following_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` = {$user_id} AND `following_id` <> {$user_id} AND `active` = '1') AND `active` = '1' ";

关注者查询

" SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `follower_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` <> {$user_id} AND `following_id` = {$user_id} AND `active` = '1') AND `active` = '1'";

最佳答案

SELECT `user_id` FROM " . T_USERS . "
WHERE `user_id` IN (SELECT CASE WHEN `following_id`={$user_id} THEN `follower_id` ELSE `following_id` END user
FROM " . T_FOLLOWERS . "
WHERE `active` = '1'
AND ((`follower_id` = {$user_id} AND `following_id` <> {$user_id})
OR (`follower_id` <> {$user_id} AND `following_id` = {$user_id}))
GROUP BY user
HAVING count(*)=2
)
AND `active` = '1'

关于php - 在关注者和关注者关系表上获取共同好友,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47365657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com