繁体   English   中英

mysql select语句中的子查询

[英]mysql a subquery in the select statement

我正在尝试编写一个查询,其中按用户使用的促销代码对用户进行排序,并告诉我这些用户中有多少拥有免费计划,有多少拥有付费计划。 这是我的尝试。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM