我有一个表 - userinfo ,其中我有一些属性 userid,firstname,lastname,EmployeeId,ReportTo
ReportTo 引用表 userinfo
中 userid 的经理我的问题是如何获得向特定经理报告的员工的数量
这是我需要获得的输出样本类型
userid FLname ReportTo NumberOfEmployees
2 abc [123] 46 456
3 xyz [323] 2 102
4 aaa [525] 3 34
我试过这个查询
select userid,concat(Firstname,' ',Lastname,' ','[',EmployeeId,']')as FLname,ReportTo,(select count(*) from userinfo where ReportTo=userid)as NumberOfEmployees from userinfo where ReportTo=3
我对计数(*)了解不多,所以我怎样才能得到结果?
答案 0 :(得分:0)
您也可以试试这个。
SELECT userid,concat(Firstname,' ',Lastname,' ','[',EmployeeId,']')as FLname,count(reportTo) as NumberOfEmployee
FROM userinfo
WHERE ReportTo=3
group by userid,firstname,lastname,EmployeeID
答案 1 :(得分:0)
试试这个
select ReportTo,(Select concat(Firstname,' ',Lastname,' ','[',EmployeeId,']') from userinfo Where userid = 5) As FLname,count(*) As NumOfEmployees from userinfo Where ReportTo = 5 Group By ReportTo
答案 2 :(得分:0)
我解决问题的方法 我试过这个查询它工作正常
select ui.userid,concat(ui.Firstname,' ',ui.Lastname,' ','[',ui.EmployeeId,']')as FLname,ReportTo,(select count(*) from userinfo ui1 where ui1.ReportTo=ui.userid)as NumberOfEmployees from userinfo ui where ReportTo=3