gpt4 book ai didi

mysql - 单个 sql 中有多个 count* sql

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:17 25 4
gpt4 key购买 nike

我想将这些 sql 语句用作单个语句,这将导致具有不同计数和 company_id 的 4 列。

select company_id,count(*) as det from feedbacks a join( select id,company_id from users  ) b on a.user_id = b.id where healthi_rating < 7 group by company_id
select company_id,count(*) as neu from feedbacks a join( select id,company_id from users ) b on a.user_id = b.id where healthi_rating <9 && healthi_rating >=7 group by company_id
select company_id,count(*) as pro from feedbacks a join( select id,company_id from users ) b on a.user_id = b.id where healthi_rating >=9 group by company_id

谁能告诉我怎么做。

最佳答案

对于 MySQL 做

select company_id,
sum(healthi_rating < 7) as det,
sum(healthi_rating between 7 and 8) as neu,
sum(healthi_rating >= 9) as pro
from feedbacks a
join( select id, company_id from users ) b on a.user_id = b.id
group by company_id

并使用 ANSI SQL 标准且没有 users 的子查询

select b.company_id,
sum(case when healthi_rating < 7 then 1 else 0 end) as det,
sum(case when healthi_rating between 7 and 8 then 1 else 0 end) as neu,
sum(case when healthi_rating >=9 then 1 else 0 end) as pro
from feedbacks a
join users b on a.user_id = b.id
group by b.company_id

关于mysql - 单个 sql 中有多个 count* sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216715/

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