select MYSQL 中用于分组依据的不同 tha 列的其他值中的值
select value among other values of a different tha column used for Group by in MYSQL
所以我有像
这样的三列
company id project id approved status
1 3 NOT
2 4 APPROVED
3 5 null
2 6 NOT
3 7 APPROVED
3 8 null
2 9 NOT
2 10 APPROVED
3 11 null
我已经使用左连接实现了这一点,现在我想要的是使用 GROUP by 对公司进行分组,条件是分组依据应该 return 第三列的值 approved_status 作为批准,如果任何公司 id 都有这个,否则 NOT 如果有 null
所以基本上我试图通过类似这种情况的方式实现第一顺序当 approved_status = 'APPROVED' 然后 1 else 2 等等 然后分组但是因为 mysql 不支持分组by order by 所以我应该怎么做才能得到这个结果..帮助!
select company_id,
case when sum(approved_status='APPROVED') > 0 then 'APPROVED'
when sum(approved_status='NOT') > 0 then 'NOT'
else null
end as `status`
from your_table
group by company_id
这里是 SQL 查询,通过计算每个公司批准的项目数量来执行您想要的操作,如果 >=1,则 returns 批准,否则不批准:
SELECT company_id, case when sum(approved_status="APPROVED")>=1 then "APPROVED" else "NOT" end
FROM Projects
GROUP BY company_id
所以我有像
这样的三列 company id project id approved status
1 3 NOT
2 4 APPROVED
3 5 null
2 6 NOT
3 7 APPROVED
3 8 null
2 9 NOT
2 10 APPROVED
3 11 null
我已经使用左连接实现了这一点,现在我想要的是使用 GROUP by 对公司进行分组,条件是分组依据应该 return 第三列的值 approved_status 作为批准,如果任何公司 id 都有这个,否则 NOT 如果有 null
所以基本上我试图通过类似这种情况的方式实现第一顺序当 approved_status = 'APPROVED' 然后 1 else 2 等等 然后分组但是因为 mysql 不支持分组by order by 所以我应该怎么做才能得到这个结果..帮助!
select company_id,
case when sum(approved_status='APPROVED') > 0 then 'APPROVED'
when sum(approved_status='NOT') > 0 then 'NOT'
else null
end as `status`
from your_table
group by company_id
这里是 SQL 查询,通过计算每个公司批准的项目数量来执行您想要的操作,如果 >=1,则 returns 批准,否则不批准:
SELECT company_id, case when sum(approved_status="APPROVED")>=1 then "APPROVED" else "NOT" end
FROM Projects
GROUP BY company_id