0

I have already written above logic using c# code but I want to handle it at database query level as optimization.

I am using mysql database The schema of department is

    Department Table
Deptid     DeptName ParentDeptid
1          CEO        -  
2          HR         1
3          IT         1
4          Dev        3
5          QA         3
6          clerk      5

Employee table
Empid    EmpName   deptid
1        E1            1
2        E2            2
3        E3            3
4        E4            4
5        E5            4
6        E6            4
7        E7            5
8        E8            5
9        E9            5
10       E10           6

I need output as if I have selected deptid is 3 I want all descendent of dept 3. It means for dept id 3

deptid  parentdept  count
3       1           1
4       3           3
5       3           3
6       5           1
10
  • Question is unclear: If you select deptid = 2 how are you expecting to get rows with deptid 3, 4 or 5 as of your first sample output table? Which other constraint should fire? Commented Jul 11, 2015 at 17:52
  • Furthermore: your last example should have the column DeptName and ParentDeptName or it is wrong allover. Commented Jul 11, 2015 at 17:54
  • Extremely Sorry I have updated question. Commented Jul 11, 2015 at 17:56
  • After edit: This row 3 1 1 doesn't fit as deptid 3 is not a descendant of itself and has a parent of 1 as all of the other actually have 3 Commented Jul 11, 2015 at 17:57
  • I need deptid 3 also in output Commented Jul 11, 2015 at 17:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.