select employee_name,count(dependent)作为员工错误的依赖项

时间:2013-11-25 06:31:55

标签: mysql

我有两个表"员工表,从属表"

员工表有" ID,employee_id,employee_name"

依赖表具有" ID,employee_id,dependent_name,relationship"

,员工表的值为("1, 123, Vincent")

和依赖表的值是

("1, 123, Angel, daughter")("2, 123, Mary, daughter")

我将如何显示employee_id和employee_name及其受抚养人数?

喜欢选择employee_id,employee_name,count(dependent)作为dependents ???

5 个答案:

答案 0 :(得分:1)

select e.employee_id, e.employee_name , (select count(*) from 
dependent_table where employee_id=e.employee_id) as dependents 
from employee_table as e 

答案 1 :(得分:1)

尝试此查询:

 select e.employee_id,e.employee_name,count(d.employee_id) as dependents
     from department d
     inner join employee e on (e.employee_id=d.employee_id) 
     group by d.employee_id;

Sql Fiddle Example

答案 2 :(得分:0)

使用分组依据和计数

select 
   em.Employee_id, 
   count(*) 
from 
   employee em inner join 
       dependant dp on 
             em.employee_id = dp.employee_id
group by 
       em.employee_id

答案 3 :(得分:0)

select 
   e.employee_id,
   e.employee_name,
   count(select * from dependent 
         table where 
        dependent table.employee_id=employee table.employee_id) 
from 
      employee table e 
        where e.employee_id=123

试试这个

答案 4 :(得分:0)

试试这个有效的

select a.empid ,a.empname , count(b.deptname) from employee a , dependent b 
where a.empid = b.empid and b.empid=123;