Bhavin
Bhavin

Reputation: 2168

parent and child database schema

I have one table named as user

USER(Table)

user_id      name     status_privilege(0=child,1=parent)    added_user_id
   2          abc                    1
   13       child_1                  0                          2
   14       child_2                  0                          2

I want display user_id = 2(parent's) data in all user_id = 14,15 (all child's login). And also I want to display user_id = 14 , 15's data in user_id = 2's data. (Viceversa)

So for that what to do ?

I tried this query.

SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id
FROM user as parent
INNER JOIN user as child ON parent.user_id = child.user_added_id
WHERE child.user_id = '14'

so I got child_user_id = 14 & parent_user_id = 2

But I want that when I put below query it give me blank result. (changed child.user_id = '2')

SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id
FROM user as parent
INNER JOIN user as child ON parent.user_id = child.user_added_id
WHERE child.user_id = '2'

So I want is that when I fire query single query then if user is parent than also I want it's child's all data in user_id ='2' means data of 14 & 15 user's. And 2nd user_id's data in all childs.

Note : All the data in other tables are from user_id so is it possible ? It's a requirement of existing project so I can not change database structure otherwise I can follow MySQL hierarchical of parent child structure.

Upvotes: 1

Views: 481

Answers (1)

Dhaval Purohit
Dhaval Purohit

Reputation: 1290

the answer is based on what i understand from the question is: you want the full array of child user ids if parent user logged in and only one parent id if child user logged in.

so in php you can derived that via

<?php
    $user=array(); // this array will contains the user info
    $res=$db->query("select * from user where user_id='$id'"); //$id the user id with which you want to check whether it is parent or child.
    if($res->num_rows > 0)
    {
        $row=$res->fetch_assoc();
        if($row['status_privilege']==0)
        {
            $res_main=$db->query("SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id FROM user as parent INNER JOIN user as child ON parent.user_id = child.user_added_id WHERE child.user_id = '".$row['user_id']."'");
            $user=$res_main->fetch_assoc();
        }
        else
        {
             $res_main=$db->query("SELECT * from user where added_user_id='".$row['user_id']."'");
             $user=$row;
             $user["child_array"]=array();
             while($row_child=$res_main->fetch_assoc())
             {
                  array_push($user['child_array'],$row_child['user_id']);
             }
        }
    }
    //you can further check that if user is parent or child by using this condition.
   if($user['status_privilege']==1)
   {
       //parent login it also contain the all child user ids
   }
   else
   {
       //child login it also contain the parent id as you have done it before.
   }
?>

Upvotes: 2

Related Questions