为什么我不能通过使用%符号执行连续命令后执行命令

问题描述:

Select category, 
     concat(count(category) * 100/ (select count(*) from table3),'%') as category_percentage_males 
from table3 
where gender in ('m') 
group by category 
order by category_percentage_males desc 

由于某种原因,一旦我连续执行命令,就不会返回desc。如果没有concat命令,则工作正常。任何理由为什么发生这种情况?为什么我不能通过使用%符号执行连续命令后执行命令

+0

能否请你告诉样本数据来说明你看到什么?我认为这是因为你期待数字排序,但他们是字符串,所以他们遵循字符串排序。 – Siyual

+0

明白!然而,当我根本不包括concat并且如下所示:count(category)* 100/select table(*)from table3)作为category_percentage我可以通过desc命令来获得前三个类别的百分比@Siyual – cpat21

+0

对 - 因为这是一个数值。 'CONCAT()'返回一个字符串值。字符串和数字排序不同。 – Siyual

这是您的查询:

Select category, concat(count(category) * 100/ (select count(*) from table3),'%') as category_percentage_males 
from table3 where gender in ('m') 
group by category 
order by category_percentage_males desc; 

我强烈建议你要求它为:

Select category, 
     concat(avg(case when gender = 'm' then 100.0 else 0 end), '%') as category_percentage_males 
group by category 
order by avg(case when gender = 'm' then 100.0 else 0 end) desc;