gpt4 book ai didi

mysql - 返回不是 friend 但一起参加最多事件的用户

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

假设我有一个如下所示的 friend 和事件表

FriendsTable {
user1_ID
user2_ID
}

Events Table {
event_ID
user_ID
}

在我的 friend 表中,如果 user1 和 user2 是 friend ,我只将它存储在我的数据库中一次,其中 user1 < user2。所以 (1,2) 会被发现,但 (2,1) 不会。

我正在尝试找出一种方法来返回不是 friend 但按降序参加相同事件的用户。例如,

Events_Table{
1, 1 //user1 attended event1
1, 2 //user2 also attended event1
3, 1 //user1 attended event 2
3, 3 //user 3 attended event 3
4, 1 //user1 attended event 4
4, 3 //user3 attended event 4
3, 5 // user 5 attended event 3
}

User_table{
1, 2 //user1 is friends with user 2
}

因此,由于用户 1 和 3 不是 friend 但一起参加了事件 3 和 4,而用户 1 和 5 不是 friend 但参加了事件 3结果将是 (1,3) 和 (1,5),因为用户 1 和用户 3 有更多类似的参加事件。

我的想法

我不想把这篇文章写得太长,但我想先用它来寻找不是 friend 的 friend

select * from (select distinct f1.user1_id as user1, f2.user2_id as
user2 from FriendsTable as f1, FriendsTable as f2 where
f1.user1_id < f2.user2_id MINUS select * from FriendsTable

然后以某种方式将它加入事件表...

最佳答案

您可以使用基于自左连接的动态表

select * from 
events where event_ID in (

select distintc e.event_ID
from events as e
left join FriendsTable as f1 on f1.user1_id = e.user_ID
left join FriendsTable as f2 on f2.user1_id = e.user_ID
where f1.user_ID is null
or f2.user_ID is null
)
order by events, user_ID;

关于mysql - 返回不是 friend 但一起参加最多事件的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40007926/

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