递归查询:函数内部不存在“表”,但函数外部存在

时间:2018-06-27 23:15:40

标签: sql mariadb

我正在尝试在Ubuntu 18.04上的MariaDB 10.3.7上的函数中包装一个递归临时表查询。我已经尝试将语句分解为更基本的部分,但是在我将所有内容放在一起之前,它都可以正常工作。

这是我要转换为函数的语句的示例:

with recursive Descendants as (
    select * from Characters where id = 91402
    union
    select c.* from Characters as c, Descendants as d
        where d.id = c.mother_id or d.id = c.real_father_id
) select count(distinct(id)) from Descendants

它打印出我想要的后代字符数。

到目前为止,这是我尝试将其转换为功能:

create function count_descendants(cid int unsigned) returns int unsigned return (
    with recursive Descendants as (
        select * from Characters where id = cid
        union
        select c.* from Characters as c, Descendants as d
            where d.id = c.mother_id or d.id = c.real_father_id
    ) select count(distinct(id)) from Descendants
);

Maria接受了,但如果我尝试使用它:

select count_descendants(91402);

它显示此错误消息:

ERROR 1146 (42S02) at line 12: Table 'ck2.Characters' doesn't exist

“ ck2”是正在使用的数据库的名称。如果有什么想看到的,我可以发布架构。

编辑:我已将其作为错误提交给MariaDB的错误跟踪器:MDEV-16629