我的目标是通过表tbl递归,并在通过该表递归时从另一个表tbl2中选择一个国家缩写(如果它存在)并将这些结果附加到最终输出中.
我将使用的例子来自这篇文章
tbl2有一个外键'tbl_id'到tbl,看起来像这样
INSERT INTO @tbl2( Id, Abbreviation, tbl_id )
VALUES
(100, 'EU', 1)
,(101, 'AS', 2)
,(102, 'DE', 3)
,(103, 'CN', 5)
Run Code Online (Sandbox Code Playgroud)
*注意:并非所有国家/地区都有缩写.
诀窍是,我希望亚洲所有国家至少显示亚洲的缩写"亚洲",即使一个国家没有缩写(例如印度).如果该国家确实有缩写,则结果需要如下所示:中国:CN,AS
我使用子查询部分工作,但印度总是为缩写返回NULL.它的行为就像没有完整的递归路径回到缩写,然后它返回null.也许解决方案是在缩写表上使用左外连接?我已经尝试了几个小时的许多不同的变化,子查询尽可能接近我.
WITH abcd
AS (
-- anchor
SELECT id, [Name], ParentID,
CAST(([Name]) AS VARCHAR(1000)) AS "Path"
FROM @tbl
WHERE ParentId IS NULL
UNION ALL
--recursive member
SELECT t.id, t.[Name], t.ParentID,
CAST((a.path + '/' + t.Name + ':' +
(
select t2.abbreviation + ','
from @tbl2
where t.id = t2.id
)) AS VARCHAR(1000)) AS …Run Code Online (Sandbox Code Playgroud) 如何在递归cte中使用排名函数?这是一个简单的例子,显示了我正在尝试做的事情:
with cte as ( select 1 a, 1 b union all select 1, 2 union all select 2, 3 union all select 2, 4 ) , rcte (a, b, c, d) as ( select a, b, cast(0 as int), 1 from cte union all select a, b, cast(ROW_NUMBER() over (partition by a order by b) as int), d+1 from rcte where d < 2 ) select * from rcte where d=2 order by a, b
为什么没有排名?告诉我我的错误
听说Perl一年后,我决定给它几个小时的时间来看看我能拿到多少.我完成了基础知识,然后进入循环.作为测试,我想看看是否可以构建一个脚本来递归所有最多4个字符的字母数字值.我写了一段PHP代码,前一段时间做了同样的事情所以我采用了相同的概念并使用它.然而,当我运行脚本时,它将"a"作为前3个值,然后仅循环通过最后一个数字.有谁看到我做错了什么?
#!/usr/local/bin/perl
$chars = "abcdefghijklmnopqrstuvwxyz";
$chars .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$chars .= "0123456789";
@charset = split(//, $chars);
$charset_length = scalar(@charset);
sub recurse
{
($width, $position, $base_string) = @_;
for ($i = 0; $i < $charset_length; ++$i) {
$base = $base_string . $charset[$i];
if ($position < $width - 1) {
$pos = $position + 1;
recurse($width, $pos, $base);
}
print $base;
print "\n";
}
}
recurse(4, 0, '');
Run Code Online (Sandbox Code Playgroud)
这是我运行时得到的:
aaaa
aaab
aaac
aaad
aaae
aaaf
aaag
aaah
aaai
aaaj
aaak
aaal
aaam
aaan …Run Code Online (Sandbox Code Playgroud) 我有一个具有递归层次结构的表(即ID,ParentID)。对于这个层次结构中的任何项目,我希望能够带回上,下层次结构的所有列表以及每一行的级别。假设父母只能生一个孩子。
例如以下内容:
ID ParentID
--------------
1 NULL
2 1
3 2
4 NULL
5 4
6 5
Run Code Online (Sandbox Code Playgroud)
给定ID 1、2或3,我想返回:
ID ParentID Level
-----------------------
1 NULL 1
2 1 2
3 2 3
Run Code Online (Sandbox Code Playgroud)
我以前做过,但是我不记得怎么做。我知道解决方案涉及CTE,但我做对了!任何帮助表示赞赏。
sql-server recursive-query common-table-expression sql-server-2008
有没有办法在SQL中发送递归查询?
给定结束节点id,我需要所有行到根节点(有parentid = NULL)按级别排序.例如,如果我有类似的东西:
nodeid | parentid
a | NULL
b | a
c | b
Run Code Online (Sandbox Code Playgroud)
在查询之后end_node_id = c,我会得到类似的东西:
nodeid | parentid | depth
a | NULL | 0
b | a | 1
c | b | 2
Run Code Online (Sandbox Code Playgroud)
(而不是深度,我也可以使用到给定端节点的距离)
我能想到的唯一(也是显而易见的)方法是每行执行一次查询,直到到达父节点.
有没有更有效的方法呢?
sql tree recursive-query hierarchical-data hierarchical-query
我试图从下到上总结 BOM 成本。我需要能够确定 BOM 的特定级别的成本,因为所有成本都是从下面的级别开始累加的。
\n\n在下面的示例中,作业 1000 成本应为以下所有作业的所有成本以及作业 1000 的成本之和。1000-1 应为 1000-1 + 1000-1A 之和,1000-2 仅包含1000-2 的成本,因为没有与该作业相关的组件,等等......
\n\n(注意:职位编号在现实世界中是随机的,无法可靠地排序。)
\n\n1000 \n 1000-1\n 1000-1a\n 1000-1B\n 1000-1B1 \n 1000-2\n 1000-3\n 1000-3A\n 1000-3B\n 1000-3B-1\n 1000-4\nRun Code Online (Sandbox Code Playgroud)\n\nBill_Of_Jobs定义装配/BOM 结构以及Job包含每个作业的成本信息的表。
在下面的示例中,我期望返回:
\n\n1000 = $150\n1000-1 = $140\n1000-1A = $ 30\n1000-1B = $ 90\n1000-1B-1 = $ 50\nRun Code Online (Sandbox Code Playgroud)\n\n\n\nCREATE TABLE [Bill_Of_Jobs]\n(\n [Parent_Job] varchar(10) NOT NULL,\n [Component_Job] varchar(10) NOT NULL,\n [Root_Job] varchar(10) NULL,\n)\n\nInsert into Bill_Of_Jobs (Parent_Job, Component_Job, Root_Job)\nValues …Run Code Online (Sandbox Code Playgroud) 我是 Laravel 的新手,我想使用 with 子句从模型中的控制器传递 $id
我的模特
class Menucategory extends Model
{
protected $fillable = ['title', 'parent_id', 'restaurant_id'];
// loads only direct children - 1 level
public function children()
{
return $this->hasMany('App\Menucategory', 'parent_id');
}
// recursive, loads all descendants
public function childrenRecursive()
{
return $this->children()->with('childrenRecursive');
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器
public function show($id)
{
$menucatagories = Menucategory::with('childrenRecursive')->where('restaurant_id',$id)->where('parent_id','0')->get();
return $menucatagories;
}
Run Code Online (Sandbox Code Playgroud)
我当前的输出是
[
{
"id": 1,
"title": "TestMenu Parant",
"parent_id": 0,
"restaurant_id": 12,
"children_recursive": [
{
"id": 2,
"title": "TestMenu SubCat1",
"parent_id": 1, …Run Code Online (Sandbox Code Playgroud) 我的名为element 的表如下所示:
id | successor | important
----------------------------
1 | NULL | 0
2 | 4 | 1
3 | 5 | 0
4 | 8 | 0
5 | 6 | 1
6 | 7 | 0
7 | NULL | 0
8 | 10 | 1
9 | 10 | 0
10 | NULL | 0
Run Code Online (Sandbox Code Playgroud)
我从一个元素的 ID 开始。每个元素可能有也可能没有后续元素。因此,给定任何元素 ID,我可以从 0..n 个元素构建一个元素链,具体取决于它的后继和后继-后继,依此类推。
假设我的起始 ID 是 2。这会导致以下链:
2 -> 4 -> 8 -> 10
Run Code Online (Sandbox Code Playgroud)
现在我想问这个问题:一个特定的元素链是否至少包含一个重要的== 1的元素?
在伪代码中,无需不必要的检查即可实现这一点的函数可能如下所示: …
我正在开发一个简单的评论系统,用户可以在其中评论其他评论,从而创建层次结构。为了按层次顺序获取评论,我在 Postgres 中使用了公共表表达式。
以下是使用的字段和查询:
id
user_id
parent_comment_id
message
WITH RECURSIVE CommentCTE AS (
SELECT id, parent_comment_id, user_id
FROM comment
WHERE parent_comment_id is NULL
UNION ALL
SELECT child.id, child.parent_comment_id, child.user_id
FROM comment child
JOIN CommentCTE
ON child.parent_comment_id = CommentCTE.id
)
SELECT * FROM CommentCTE
Run Code Online (Sandbox Code Playgroud)
上面的查询以广度优先的方式返回记录:
id parent_comment_id user_id
10 null 30
9 null 30
11 9 30
14 10 31
15 10 31
12 11 30
13 12 31
Run Code Online (Sandbox Code Playgroud)
但是是否可以修改它以实现如下所示的内容,其中以深度优先的方式为该评论集一起返回记录?重点是通过这种方式获取数据,让前端渲染更加流畅。
id parent_comment_id user_id
9 null 30
11 9 30
12 11 …Run Code Online (Sandbox Code Playgroud) 我在c语言中看到了一个递归函数示例.
#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Run Code Online (Sandbox Code Playgroud)
但在这里我无法理解sum函数实际上只返回0,从代码可见,它将返回自己加n.
那么为什么函数调用被转换为数字,函数中没有行告诉它本身也返回n除了它是0.