如何构建自动循环PHP - SQl

时间:2013-01-30 11:00:00

标签: php sql

我有两张桌子:

1. Team
id  group_head  initial_team
id : for id table (auto increment)
Group Head : employee id to add group head
initial team : initial team for this team


2. Team Sub
id  team_id     employee_id     parent  parent_id
id : id for table (auto increment)
team_id : id of the Team_table
employee_id : employee id
parent : value 0 if they child from team_sub, value 1 if they child from team
parent_id : if parent value 1 the parent id is id team, if parent value 0 the parent is id team sub

我的问题: 如何从团队(团队负责人)了解全部孩子?或者如何从id团队中了解最底层的孩子?如果我删除id团队,孩子也会被删除..

示例:

id team : 1

data from team_ sub :
status parent: 1 parent_id : 1 (id this data is 1)
status parent: 0 parent_id : 1 (id this data is 2)
status parent: 0 parent_id : 2 (id this data is 3)
status parent: 0 parent_id : 3 (id this data is 4)
status parent: 0 parent_id : 4 (id this data is 5) 

id team : 2

data from team_ sub :
status parent: 1 parent_id : 2 (id this data is 6)
status parent: 0 parent_id : 6 (id this data is 7)
status parent: 0 parent_id : 7 (id this data is 8)
status parent: 0 parent_id : 8 (id this data is 9)
status parent: 0 parent_id : 9 (id this data is 10) 

example on image

其中父状态为1,parent_id为id团队。
如果父状态为0,则parent_id为id team_sub ..

这是关于图像的关系数据 enter image description here
抱歉,如果我的英语不好......

id team_sub 10是来自id team 2的孩子,id team_sub 5是来自id team 1的孩子。

1 个答案:

答案 0 :(得分:0)

现在无法检查,但如果我理解你的话,应该是这样的:

SELECT * FROM Team_Sub WHERE (parent_id = 0)

它选择技术上属于sub_team的parent_id = 1 aka部分的每个条目

如果您只是想要一些具有这种条件的托管:

SELECT COUNT(*) FROM Team_Sub WHERE (parent_id = 0)

exaples:

//Count all entries in Team_ID 1:

SELECT COUNT(*) FROM Team_Sub WHERE (parent_id = 1) 


//Select all entrys in Team_ID 2:


SELECT * FROM Team_Sub WHERE (parent_id = 2) 


//Count all Entries that do belong to an Team_ID:

SELECT COUNT(*) FROM Team_Sub WHERE NOT (parent_id = 0) 


//Select all Entries that do belong to an Team_ID:

SELECT * FROM Team_Sub WHERE NOT (parent_id = 0)

我建议遵循以下结构:

status parent: 1 parent_id : 2 (id this data is 6)
status parent: 0 parent_id : 2 (id this data is 7)
status parent: 0 parent_id : 2 (id this data is 8)
status parent: 0 parent_id : 2 (id this data is 9)
status parent: 0 parent_id : 2 (id this data is 10) 

通过这种方式,您可以通过“parent_id”确定团队,并通过“父母”确定团队负责人。

所以在上面这样的例子中:

id 6 is Head of Team 2
id 7 is Subordinate of Team 2
id 8 is Subordinate of Team 2
id 9 is Subordinate of Team 2
id 10 is Subordinate of Team 2

通过这个,你可以通过简单地确定“parent_id”和“parent”

来查询你想要的任何条目

e.g:

//Just Heads of any Team:
SELECT * FROM Team_Sub WHERE (parent = 1)

//Just Head of a specific team:
SELECT * FROM Team_Sub WHERE (parent = 1) AND (parent_id="team id")

//Just Subordinates of any Team:
SELECT * FROM Team_Sub WHERE (parent = 0)

//Just Subordinates of a specific team:
SELECT * FROM Team_Sub WHERE (parent = 0) AND (parent_id="team id")

//Just Anybody of a specific team:
SELECT * FROM Team_Sub WHERE (parent_id="team id")

* 仅供参考:如果还没有依赖于值名称,则ID建议将“parent”重命名为“head_of_team”,将“parent_id”重命名为“team_id”*