我创建了一个简单的例子来说明PostgreSQL中使用递归查询的传递闭包.
但是,我的递归查询有些问题.我不熟悉语法,所以这个请求可能完全是我的noobish,为此我提前道歉.如果运行查询,您将看到节点1在路径结果中重复自身.有人可以帮我弄清楚如何调整SQL吗?
/* 1
/ \
2 3
/ \ /
4 5 6
/
7
/ \
8 9
*/
create table account(
acct_id INT,
parent_id INT REFERENCES account(acct_id),
acct_name VARCHAR(100),
PRIMARY KEY(acct_id)
);
insert into account (acct_id, parent_id, acct_name) values (1,1,'account 1');
insert into account (acct_id, parent_id, acct_name) values (2,1,'account 2');
insert into account (acct_id, parent_id, acct_name) values (3,1,'account 3');
insert into account (acct_id, parent_id, acct_name) values (4,2,'account 4');
insert into account (acct_id, parent_id, acct_name) values (5,2,'account 5'); …Run Code Online (Sandbox Code Playgroud) sql postgresql recursive-query common-table-expression transitive-closure-table
I have the following three tables in SQL:
select * from movie limit 2;
id | title | year | content_rating | duration | lang | country | gross | budget | director_id
------+----------------------------+------+----------------+----------+------------+----------------------+----------+----------+-------------
407 | 102 Dalmatians | 2000 | G | 100 | English | USA | 66941559 | 85000000 | 2174
3699 | 10 Cloverfield Lane | 2016 | PG-13 | 104 | English | USA | 71897215 | 15000000 | 1327
(2 rows)
Run Code Online (Sandbox Code Playgroud)
select * from …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个递归构建特定类别路径的函数
CREATE FUNCTION getPath(inId INT)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE return_path TEXT;
DECLARE return_parent_id INT;
SELECT CONCAT('/', name) INTO return_path FROM article_categories WHERE id = inId;
SELECT parent_id INTO return_parent_id FROM article_categories WHERE id = inId;
IF return_parent_id > 0 THEN
SELECT CONCAT(getPath(return_parent_id), return_path) INTO return_path;
END IF;
RETURN return_path;
END
Run Code Online (Sandbox Code Playgroud)
当我尝试使用没有父项的类别(parent_id = 0)运行此函数时,它工作正常,但是当我尝试一个parent_id> 0的类别时,我得到1424递归存储函数和触发器是不允许的.
我该如何解决这个问题?我将在常规的Web托管服务上托管此代码,该服务至少应该具有MySQL服务器版本5.1.
在Ike Walker的帮助下,我做了一个很好的工作
DROP PROCEDURE IF EXISTS getPath;
DELIMITER //
CREATE PROCEDURE getPath(IN category_id INT UNSIGNED, OUT return_path TEXT)
BEGIN
DECLARE parent_id INT UNSIGNED;
DECLARE …Run Code Online (Sandbox Code Playgroud) 我有一个包含如下值的表
?????????????????????????????
? b ? l1 ? l2 ? l3 ? l4 ?
?????????????????????????????
? a ? b1 ? c1 ? d1 ? e1 ?
? d ? x1 ? y1 ? null ? null ?
?????????????????????????????
Run Code Online (Sandbox Code Playgroud)
输出应该是:
?????????????
? ab1c1d1e1 ?
? ab1c1d1 ?
? ab1c1 ?
? ab1 ?
? dx1y1 ?
? dx1 ?
?????????????
Run Code Online (Sandbox Code Playgroud)
可能吗?我在这里看到一个模式,但能够弄清楚如何做到这一点.PS:ROLLUP不能用作服务器不支持它.
对不起 - 这有点含糊......
这里:(在 CTE 中找到无限递归循环)讨论了如何防止递归查询中出现无限循环。在“查询级别”上阻止了递归 - 至少在关于 Postgresql 的答案中是这样。
Postgresql (10) 有没有办法实现某种安全网来防止无限递归?这是一种可行的方法,statement_timeout还是有其他广泛接受的方法?
我正在寻找建立一个允许使用分层过滤查询数据的工具.我有一些想法,我将如何去做,但想知道是否有任何建议或建议可能更有效.
例如,假设用户正在搜索作业.工作领域如下.
1: Scotland
2: --- West Central
3: ------ Glasgow
4: ------ Etc
5: --- North East
6: ------ Ayrshire
7: ------ Etc
Run Code Online (Sandbox Code Playgroud)
用户可以搜索特定的(即格拉斯哥)或更大的区域(即苏格兰).
我正在考虑的两种方法是:
SELECT * FROM Jobs WHERE Category IN Areas.childrenField.我从两者看到的问题是:
关于最佳方法的任何想法,建议或建议?我正在使用C#ASP.NET和MSSQL 2005 DB.
我有一个两表分层设置,其中表A引用表B,然后引用表A中的不同记录,依此类推......但仅限于给定的递归深度.
我使用SQLAlchemy和声明性工作得很好.我也成功地使用表格关系lazy和join_depth属性的热切加载.这是根据SQLAlchemy文档.
但是,这种安排将递归深度固定在' join_depth'程序加载时间一次......但是对于我正在使用的数据,我知道每次应该使用的递归深度. 如何更改基于每个查询的递归深度?
我考虑过摆弄join_depth基础ORM对象上的master 属性,但这不起作用,因为我有一个多线程的scoped_session应用程序,这将是危险的(更不用说参数很难到在运行时位于SQLAlchemy内!).
我也看过使用joinedload查询,但没有看到如何改变深度.
我也知道WITH RECURSIVE通过CTE在一些数据库中提供的' 'SQL语法,但是尽管如此,我想暂时避免这种情况,因为一些数据库仍然不支持它(SQLAlchemy也没有 - 至少不是现在而且没有很多方言定制).
WITH RECURSIVE我是PostgreSQL 的新手。我有一个相当标准的递归查询,它遵循邻接列表。如果我有,例如:
1 -> 2
2 -> 3
3 -> 4
3 -> 5
5 -> 6
Run Code Online (Sandbox Code Playgroud)
它产生:
1
1,2
1,2,3
1,2,3,4
1,2,3,5
1,2,3,5,6
Run Code Online (Sandbox Code Playgroud)
我想要的是:
1,2,3,4
1,2,3,5,6
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在 Postgres 中做到这一点。这似乎是“选择最长的路径”或“选择不包含在另一条路径中的路径”。我也许可以看到如何通过连接本身来做到这一点,但这似乎效率很低。
一个示例查询是:
WITH RECURSIVE search_graph(id, link, data, depth, path, cycle) AS (
SELECT g.id, g.link, g.data, 1, ARRAY[g.id], false
FROM graph g
UNION ALL
SELECT g.id, g.link, g.data, sg.depth + 1, path || g.id, g.id = ANY(path)
FROM graph g, search_graph sg
WHERE g.id = sg.link AND NOT cycle …Run Code Online (Sandbox Code Playgroud) sql postgresql directed-graph recursive-query common-table-expression
如何返回AllSubSections(所有级别)的所有 ID
class Section extends Model
{
public function Ads()
{
return $this->hasMany(Ad::class);
}
public function AllSubSections()
{
return $this->SubSections()->with('AllSubSections');
}
public function SubSections()
{
return $this->hasMany(Section::class);
}
public function Parent()
{
return $this->belongsTo(Section::class);
}
}
Run Code Online (Sandbox Code Playgroud)
我目前正在做的是:
$section = Section::where('name', 'Properties')->first();
$subSections = $section->AllSubSections;
$subSections->pluck('id')
Run Code Online (Sandbox Code Playgroud)
但它只返回第一级而不是所有级别。
我有这种情况使用Mysql上的递归查询在一个表上找到lv 2和lv3子...
我正在使用的数据库结构:
id name parent
1 A 0
2 B 0
3 C 0
4 D 1
5 E 1
6 F 2
7 G 2
8 H 3
9 I 3
10 J 4
11 K 4
Run Code Online (Sandbox Code Playgroud)
我期望的结果,当过滤数据时,id = 1,它将产生我期待的结果.
id name parent
4 D 1
5 E 1
10 J 4
11 K 4
Run Code Online (Sandbox Code Playgroud)
我一直在寻找各地,并阅读这个http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/,但我没有找到我正在寻找的结果..
任何帮助将不胜感激, 谢谢
recursive-query ×10
sql ×5
mysql ×4
postgresql ×4
php ×2
asp.net ×1
c# ×1
hierarchy ×1
laravel ×1
laravel-5.3 ×1
python ×1
recursion ×1
sql-server ×1
sqlalchemy ×1