concatenationcommon-table-expressionmariadb-10.3

CTE CONCAT not joining strings


I'm new to SQL. I'm using MySQL Ver 15.1 Distrib 10.3.27-MariaDB. For the following. I'm trying to compute paths in a graph like this: a->b.

create table edges
(
    start varchar(4),
    end   varchar(4)
);
insert edges(start, end) values ("a", "b");

with recursive cte (path, last) as
(
    select "a", "a"
    union all
    select CONCAT(path, "->", edges.end), edges.end from cte, edges
           where cte.last = edges.start
)
select * from cte;

I was expected to get two rows like this [("a", "a"), ("a->b", "b")] but instead got [("a", "a"), ("a", "b")]. Why was there no concatenation?


Solution

  • datatype and length are derived from the first select you should be seeing an error

    try

    with recursive cte (path, last) as
    (
        select cast("a" as char(100)), cast("a" as char(100))
        union all
        select CONCAT(path, "->", edges.end), edges.end from cte, edges
               where cte.last = edges.start
    )
    select * from cte;
    

    https://dbfiddle.uk/WAz2Jjg0