Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询用于网络分析的6个分离度_Sql_Postgresql - Fatal编程技术网

SQL查询用于网络分析的6个分离度

SQL查询用于网络分析的6个分离度,sql,postgresql,Sql,Postgresql,我正在使用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.pla

我正在使用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度的间隔进行连接。

我想应该是:

-- 6 degrees of separation
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 hash IN
 (SELECT hash FROM game.phone_hashes
  WHERE hash IN
  (SELECT hash FROM game.phone_hashes
   WHERE hash IN
   (SELECT hash FROM game.phone_hashes
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes
     WHERE hash IN
     (SELECT hash FROM game.phone_hashes
      WHERE player_id = $1))))));
请参见以下工作:

-- 1 degree of separation
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);

-- 2 degrees of separation
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 hash IN
 (SELECT hash FROM game.phone_hashes
  WHERE player_id = $1));

-- 3 degrees of separation
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 hash IN
 (SELECT hash FROM game.phone_hashes
  WHERE hash IN
  (SELECT hash FROM game.phone_hashes
   WHERE player_id = $1)));

-- 4 degrees of separation
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 hash IN
 (SELECT hash FROM game.phone_hashes
  WHERE hash IN
  (SELECT hash FROM game.phone_hashes
   WHERE hash IN
   (SELECT hash FROM game.phone_hashes
    WHERE player_id = $1))));


-- 5 degrees of separation
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 hash IN
 (SELECT hash FROM game.phone_hashes
  WHERE hash IN
  (SELECT hash FROM game.phone_hashes
   WHERE hash IN
   (SELECT hash FROM game.phone_hashes
    WHERE hash IN
    (SELECT hash FROM game.phone_hashes
     WHERE player_id = $1)))));

-- 6 degrees of separation
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 hash IN
 (SELECT hash FROM game.phone_hashes
  WHERE hash IN
  (SELECT hash FROM game.phone_hashes
   WHERE hash IN
   (SELECT hash FROM game.phone_hashes
    WHERE hash IN 
    (SELECT hash FROM game.phone_hashes
     WHERE hash IN
     (SELECT hash FROM game.phone_hashes
      WHERE player_id = $1))))));

我想这就是你的意思:

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

请编辑您的问题并添加所涉及表的定义创建表。此外,如果问题包含示例—几行数据以及查询结果应基于此示例数据,则通常有助于理解问题。表定义是通过psql中的\d game.phone\u散列得到的。或完整的创建表脚本。