这是一个使用sql CTE创建包含关系的表的传递闭包的查询 用户之间。(图表的边缘)。 怎么能在mySQL中完成呢?
WITH RECURSIVE transitive_closure(a, b, distance, path_string) AS ( SELECT a, b, 1 AS distance, a || '.' || b || '.' AS path_string FROM edges WHERE a = 1 -- source UNION ALL SELECT tc.a, e.b, tc.distance + 1, tc.path_string || e.b || '.' AS path_string FROM edges AS e JOIN transitive_closure AS tc ON e.a = tc.b WHERE tc.path_string NOT LIKE '%' || e.b || '.%' ) SELECT * FROM transitive_closure ORDER BY a, b, distance;
或者至少找到两个节点之间的所有简单路径,而不为所有节点创建传递闭包。 代码取自:http://techportal.inviqa.com/2009/09/07/graphs-in-the-database-sql-meets-social-networks/