select
sum(case when category=99 then 1 end) as 'admin',
sum(case when category=1 then 1 end) as 'formal',
sum(case when category=2 then 1 end) as 'factory'
from users
select
count(*) as sum,
case
when category=99 then 'admin'
when category=1 then 'formal'
when category=2 then 'factory'
else category
end
from users
group by category;
select
count(*) as sum,
case
when category=99 then 'admin'
when category=1 then 'formal'
when category=2 then 'factory'
else category
end
from users
group by category;
这个语句曾经使用过,比用 ruby 的语法来判断效率高太多
是的呢 做分类统计用这个比较合适,不然会出现大量的遍历。
select
count(*) as sum,
case
when category=99 then 'admin'
when category=1 then 'formal'
when category=2 then 'factory'
else category
end
from users
group by category;
👍