Ben
Ben

Reputation: 62366

MySQL function to pull parent instead of current row

There's a function in MySQL that I can't remember what it's called.... if you have the following table, it'll pull the data from the parent_id row instead of the current row when the parent_id is not 0

    id     parent_id      name
-------------------------------------------
    1      0              MyRow1
    2      1              MyRow2

And... is it possible for me to determine if it's pulling the parent row or the current row by flagging a used_reference to 1 or 0?

Upvotes: 1

Views: 83

Answers (1)

Quassnoi
Quassnoi

Reputation: 425331

There is no such function, but you can do it like this:

SELECT  IF(c.parent_id = 0, p.name, c.name),
        c.parent_id <> 0 AS used_reference
FROM    mytable c
LEFT JOIN
        mytable p
ON      p.id = c.parent_id

Upvotes: 3

Related Questions