簡體   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