我有此数据库表:membership

id   name       email               level      memberdate
1    Joe        [email protected]       silver     04/09/2015
2    Peter      [email protected]    bronze     05/09/2015
3    John       [email protected]      gold       06/09/2015
4    Jack       [email protected]      bronze     09/09/2015


我首先使用此查询来显示等级为青铜的人。

select *
from membership
order by
  case
    when level = 'bronze' then 1
    else 2
  end,
  memberdate desc;


此外,我想计算在上面的同一查询中有多少人的水平= bronze。在这种情况下,计数结果应为2。我怎样才能做到这一点?

谢谢

最佳答案

select *, (select sum(level = 'bronze') from membership) as bronze_count
from membership
order by case when level = 'bronze' then 1 else 2 end,
         memberdate desc

关于mysql - 当...查询时,MySQL按一种情况选择并计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32922792/

10-11 01:27