Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all data of parent child relation-ship from same table in mysql

Tags:

mysql

I am trying to get all rows which meets the parent child relation ship. for example

  id         Title    parent_id
  1200        A       1000
  1201        B       1000
  1202        C       1000
  1203        D       1000
  1204        E       1200
  1205        F       1200
  1206        G       1201
  1207        H       1205
  1208        I       1205
  1209        J       1205

Now i have 1209 id, I want to retrieve all rows which satisfy parent child relation ship for 1209. Like here 1209 relates to 1205, 1205 relates to 1200, and 1200 relates to 1000.

I tried this query

  SELECT * FROM `category` a left join category b on a.id=b.parent_id where a.id=1209

But this is giving me only one record. We are trying to get all rows which comes in this relationship.

like image 648
ISHAN DOGRA Avatar asked Apr 09 '16 22:04

ISHAN DOGRA


People also ask

How do I get all parent children in SQL?

level + 1 FROM pc a JOIN cte c ON a. parent = c. child ) SELECT distinct parent, child , level FROM cte order by level, parent; This will give you all descendants and the level.

How do I find parent/child relations in SQL?

To find out who that child's parent is, you have to look at the column parent_id , find the same ID number in the id column, and look in that row for the parent's name. In other words, Jim Cliffy has no parents in this table; the value in his parent_id column is NULL .

How do you find parent table and child table?

The child table would have one or more columns which relate to one or more columns on the parent table. The parent table column(s) must have a primary or unique constraint place on them.


1 Answers

if you're just looking for it's parent,grandparent,greatgrand parent you can use something like this.

SELECT id,title,parent_id FROM
    (SELECT id,title,parent_id,
       CASE WHEN id = 1209 THEN @id := parent_id
            WHEN id = @id THEN @id := parent_id
            END as checkId
     FROM Test
     ORDER BY id DESC) as T
WHERE checkId IS NOT NULL

sqlfiddle

And just in case if you wanted to find all children, and grand children or great grand children of an id you can use this

SELECT id,title,parent_id FROM
    (SELECT id,title,parent_id,
            CASE WHEN id = 1200 THEN @idlist := CONCAT(id)
                 WHEN FIND_IN_SET(parent_id,@idlist) THEN @idlist := CONCAT(@idlist,',',id)
            END as checkId
     FROM Test
     ORDER BY id ASC) as T
WHERE checkId IS NOT NULL

sqlfiddle for finding children

query for finding all parents/grandparents/greatgrandparents of multiple children

SELECT id,title,parent_id FROM
(SELECT id,title,parent_id,
       CASE WHEN id in (1209,1206) THEN @idlist := CONCAT(IFNULL(@idlist,''),',',parent_id)
            WHEN FIND_IN_SET(id,@idlist) THEN @idlist := CONCAT(@idlist,',',parent_id)
            END as checkId
FROM Test
ORDER BY id DESC)T
WHERE checkId IS NOT NULL

sqlfiddle

like image 148
Tin Tran Avatar answered Nov 03 '22 00:11

Tin Tran