0

我有 2 张桌子:

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 team的底层孩子?如果我删除 id team 孩子也会被删除..

例子 :

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) 

图片上的示例

其中父状态为 1,parent_id 为 id team..
父状态为 0,parent_id 为 id team_sub..

这是图像上的关系数据, 在此处输入图像描述
如果我的英语不好,抱歉...

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

4

1 回答 1

0

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

SELECT * FROM Team_Sub WHERE (parent_id = 0)

它选择技术上属于 parent_id =1 的每个条目,也就是 sub_team 的一部分

如果您只想要一些具有此条件的条目:

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

例子:

//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”来查询你想要的任何条目

例如:

//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”*

于 2013-01-30T11:48:23.013 回答