gpt4 book ai didi

网络分析的SQL查询6度分离

转载 作者:行者123 更新时间:2023-11-29 11:19:04 24 4
gpt4 key购买 nike

我正在使用 D3.js 构建网络分析,以在我的应用程序中显示低至六度分离的连接电话号码。用于查找初始连接的 SQL (postgres) 在下面并且相当简单。但是,我对如何修改它以遍历六个级别的连接然后停止感到困惑。

SELECT player_id, ps.player_state, ps.email, ph.create_date
FROM game.phone_hashes ph
INNER JOIN game.customer_settings cs ON cs.id = ph.player_id
WHERE hash IN (SELECT hash FROM game.phone_hashes WHERE player_id = $1);

通过对这个问题的研究,我发现了对公用表表达式 (CTE) 和递归的提及,但我不确定如何在此处应用它们。

我的目标是让所有玩家通过一个公共(public)电话哈希连接到初始玩家 ($1),然后所有玩家通过一个公共(public)电话哈希连接到每个连接,如此循环往复到 6 度分离。

最佳答案

我想这就是你的意思:

with recursive tc as(
select $1 as player_id, 1 as level
union
select ph2.player_id, level+1
from tc, phone_hashes ph1, phone_hashes ph2
where tc.player_id=ph1.player_id
and ph1.hash=ph2.hash
and tc.level < 6
)
select distinct player_id from tc

关于网络分析的SQL查询6度分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33814857/

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