我正在尝试编写一个查询,其中按用户使用的促销代码对用户进行排序,并告诉我其中有多少人有免费计划,有多少人有付费计划。这是我的尝试。
select count(refer) as promo, (count(plan)
from users
where plan = 'free'
) as free, (count(plan)
from users
where plan <> 'free'
) as pay from users
where refer <> ''
group by refer
这是行不通的,但是我不确定要进行哪些更改才能使其正常工作。
最佳答案
通过功能CASE可以
select count(refer) as promo,
count(case when plan = 'free' then 1 end) as free
count(case when plan <> 'free' then 1 end) as pay
from users
where refer <> ''
group by refer
关于mysql - mysql select语句中的子查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46458220/