Displaying rows with count 0 with mysql group by

后端 未结 3 1774
甜味超标
甜味超标 2020-12-09 11:00

I have two tables in MySql Company : (cname,city) works : (ename,cname,salary)

I want to display number of employees working for every company, even if that number i

相关标签:
3条回答
  • 2020-12-09 11:48

    Try

    Select company.cname, count(work.id) from company left join work on ....=.... group by company.cname
    

    where you fill out the "...." parts, and change the work.id to you name

    0 讨论(0)
  • 2020-12-09 11:56

    Classic case for a LEFT JOIN:

    SELECT
      c.cname,
      COUNT(w.ename) wcount 
    FROM
      company c
      LEFT JOIN works w ON c.cname = w.cname
    GROUP BY
      c.cname
    
    0 讨论(0)
  • 2020-12-09 11:59

    There is another way to do so with subquery, here is a sample from my case:

    select count(`order_id`) as cnt
    from (
        select `order_id` from `room_bookings`
        where `room_id` = 3 and `day_id` = 20180201 
        group by `order_id`
    ) as b;
    
    0 讨论(0)
提交回复
热议问题