我正在使用 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)和递归,但我不确定如何在这里应用它们。
我的目标是让所有玩家通过共同的电话哈希连接到初始玩家(1 美元),然后所有玩家通过共同的电话哈希连接到每个连接,一直到 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)