gpt4 book ai didi

mysql - 我需要有关 MySQL 查询的帮助

转载 作者:可可西里 更新时间:2023-11-01 07:50:14 25 4
gpt4 key购买 nike

I have two tables - `employee` and `department`. 

1. `employee` table contains column id,employee name and dept_id
2. `department` table contains column id, department name.

I need exact department name which contains

1. maximum employee and
2. no employee

编辑:

Apologizing for bad grammar, here is the example for above two questions what i need.
1. for eg: if two department contains same number of employees, i need to show both department not single by limit.
2. for eg: if more than one department contains 0 employees, i must show those departments particularly.

最佳答案

select department_name as `department name`, 
count(*) as `number of employees`
from employee
inner join department
on employee.dept_id = department.id
group by department_name
order by count(*) desc
limit 1

我认为应该这样做。我有一段时间没有用 mysql 做过任何事情。

编辑:错过了第二个问题

select department_name as `department name`, 
count(*) as `number of employees`
from employee
left join department
on employee.dept_id = department.id
group by department_name
HAVING count(*) = 0

关于mysql - 我需要有关 MySQL 查询的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6053902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com