[英]parent and child database schema
I have one table named as user
我有一个表名为user
USER(Table) USER(表)
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). 我想在所有user_id = 14,15
(所有孩子的登录名)中显示user_id = 2
(父母的)数据。 And also I want to display user_id = 14 , 15
's data in user_id = 2
's data. 我也想在user_id = 2
的数据中显示user_id = 14 , 15
的数据。 (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 所以我得到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'
) (更改后的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. 所以我想要的是,当我对单个查询进行查询时,如果用户是父级,那么我希望它也是子级的所有数据user_id ='2'
表示14和15个用户的数据。 And 2nd user_id's data in all childs. 还有所有子代中的第二个user_id的数据。
Note : All the data in other tables are from user_id
so is it possible ? 注意:其他表中的所有数据均来自user_id
这样可以吗? It's a requirement of existing project so I can not change database structure otherwise I can follow MySQL hierarchical of parent child structure. 这是现有项目的要求,因此我不能更改数据库结构,否则可以遵循父子结构的MySQL层次结构。
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. 答案是基于我从问题中得出的结论:如果父用户登录,则需要完整的子用户ID数组;如果子用户登录,则只需要一个父ID。
so in php you can derived that via 所以在PHP中,您可以通过导出
<?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.
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.