列出所有至少拥有 3 名员工的老板

发布于 2025-01-07 13:33:18 字数 495 浏览 0 评论 0原文

如何查询下表获取所有boss的姓名(即boss_id=staff_id) 谁至少有 3 名员工?我是否使用havinggroup by、...?

    CREATE TABLE STAFF (STAFF_ID                    CHAR(3),
                        STAFF_NAME              CHAR(20),
                        GENDER                  CHAR(6),
                        DEPARTMENT              CHAR(20),
                        BOSS_ID                 CHAR(3)
                        SALARY                  NUMBER(8,2))

谢谢!

How can I query the table below to get the names of all bosses (ie boss_id = staff_id)
who have at least 3 staff? Do I use having, group by, ...?

    CREATE TABLE STAFF (STAFF_ID                    CHAR(3),
                        STAFF_NAME              CHAR(20),
                        GENDER                  CHAR(6),
                        DEPARTMENT              CHAR(20),
                        BOSS_ID                 CHAR(3)
                        SALARY                  NUMBER(8,2))

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2025-01-14 13:33:18

我使用 COUNT(*)>3 因为我假设,您的意思是除老板本人之外的 3 名员工。

SELECT a.staff_name
FROM staff a
INNER JOIN (
  SELECT boss_id,COUNT(*) AS cnt_staff FROM staff GROUP BY boss_id HAVING COUNT(*)>3
) b ON a.boss_id=b.boss_id
WHERE a.staff_id=a.boss_id

I'm using COUNT(*)>3 because I assume, that you mean 3 staff except the boss himself.

SELECT a.staff_name
FROM staff a
INNER JOIN (
  SELECT boss_id,COUNT(*) AS cnt_staff FROM staff GROUP BY boss_id HAVING COUNT(*)>3
) b ON a.boss_id=b.boss_id
WHERE a.staff_id=a.boss_id
吾性傲以野 2025-01-14 13:33:18
select S.Boss_ID, X.STAFF_NAME
from STAFF S cross apply (Select staff_name from STAFF S2 where S2.staff_id=S.boss_ID) X
group by S.Boss_ID, X.STAFF_NAME
having count(*)>=3
select S.Boss_ID, X.STAFF_NAME
from STAFF S cross apply (Select staff_name from STAFF S2 where S2.staff_id=S.boss_ID) X
group by S.Boss_ID, X.STAFF_NAME
having count(*)>=3
无法回应 2025-01-14 13:33:18
select boss_id, staff_name 
from staff 
group by boss_id, staff_name 
having count(staff_id)>=3
select boss_id, staff_name 
from staff 
group by boss_id, staff_name 
having count(staff_id)>=3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文