数据库架构看起来像
雇员(employee_name,街道,城市)
作品(employee_name,COMPANY_NAME,工资)
公司(COMPANY_NAME,市)
管理(employee_name,MANAGER_NAME)
需要做的查询是:
找到拥有最多员工的公司。
我可以通过查询找出最大数量:
SELECT max( cnt ) max_cnt
FROM (
SELECT count( employee_name ) cnt, company_name
FROM works
GROUP BY company_name
)w1;
但现在我找不到公司的名称。如果有人有想法请分享。
答案 0 :(得分:4)
要获得包含最大值的整行,您可以使用ORDER BY ... DESC LIMIT 1
代替MAX
:
SELECT company_name, cnt
FROM (
SELECT company_name, count(employee_name) AS cnt
FROM works
GROUP BY company_name
) w1
ORDER BY cnt DESC
LIMIT 1
答案 1 :(得分:2)
如下:
SELECT count( employee_name ) cnt, company_name
FROM works
GROUP BY company_name
ORDER BY cnt DESC
LIMIT 1;
编辑:
上面修正了MySQL
答案 2 :(得分:2)
SELECT company_name,count(*) as cnt
FROM works
GROUP BY company_name
ORDER BY cnt DESC
答案 3 :(得分:1)
select company_name
from works
group by company_name
having count(distinct employee_name)>=all(select count(distinct employee_name)
from works
group by company_name )
答案 4 :(得分:0)
这是工作查询
Select * from(SELECT count(EmpName)cnt, CName FROM works GROUP BY CName Order By cnt desc) where ROWNUM = 1;
答案 5 :(得分:0)
这看起来像是课程问题。
如果不止一家公司拥有相同数量的员工,则LIMIT的查询无效。 " ORDER BY"没有过滤掉无用的信息。因此,我们有以下解决方案
SELECT company_name FROM
(SELECT company_name, count(employee_name) cnt
FROM works
GROUP BY company_name)
JOIN
(SELECT max(cnt) max_cnt
FROM (
SELECT count(employee_name) cnt
FROM works
GROUP BY company_name
)) ON cnt = max_cnt
答案 6 :(得分:-1)
select company_name from works_for
group by company_name
having count(employee_name) = (select max(count(employee_name))from works_for
group by company_name);