0

I looked at How to get lowest, common parent for 2 rows in recursive table (SQL) where they suggest:

WITH
    hier1 (id, parent) AS (
    SELECT      id, parent
    FROM        table
    WHERE       id = @user1
    UNION ALL
    SELECT      id, parent
    FROM        table l, hier1 h
    WHERE       l.id = h.parent
    ),
    hier2 (id, parent) AS (
    SELECT      id, parent
    FROM        table
    WHERE       id = @user2
    UNION ALL
    SELECT      id, parent
    FROM        table l, hier1 h
    WHERE       l.id = h.parent
    )
SELECT  TOP 1 hier1.id
FROM    hier1, hier2
WHERE   hier1.id = hier2.id

and I changed to match my stuff:

WITH hier1(ID, motherID, fatherID) AS (
    SELECT ID, motherID, fatherID
    FROM persons
    WHERE ID = 3 -- example
    UNION ALL
    SELECT ID, motherID, fatherID
    FROM persons l, hier1 h
    WHERE l.ID = h.motherID OR l.ID = h.fatherID),
    hier2(ID, motherID, fatherID) AS(
    SELECT ID, motherID, fatherID
    FROM persons
    WHERE ID = 17 -- example
    UNION ALL
    SELECT ID, motherID, fatherID
    FROM persons l, hier1 h
    WHERE l.ID = h.motherID OR l.ID = h.fatherID)
SELECT hier1.ID
FROM hier1, hier2
WHERE hier1.ID = hier2.ID
LIMIT 1

but get:

Table 'familytree.hier1' doesn't exist

It also says:

Unrecognized statement type. (near "WITH" at position 0)

I use MariaDB. MariaDB documentation for WITH

I tried looking for answers here: Oracle WITH CLAUSE not working?

Seems like it works for some, and not for some..? I would appreciate some guidance!

Thank you!

Robbas
  • 39
  • 1
  • 9

0 Answers0