简体   繁体   English

在MySQL中按首字母选择最大数量组

[英]Select max number group by initial letter in MySQL

I have column say, Process in MySQL table with values like A1,A2,..A100, B5,B7..B700, C2,C3...C900 each in separate row. 我有专栏说,MySQL表中的Process分别在单独的行中具有A1,A2,.. A100,B5,B7..B700,C2,C3 ... C900之类的值。 I want to find max number from each initial. 我想从每个缩写中找到最大数量。 so output will be as 所以输出将是

A100, B700,C900

I am using below query 我正在使用以下查询

select MAX(process) as max_process
from tablename
group by substr(process,1,1)

but it don't gives me max number 但这没有给我最大数量

You can try below - 您可以在下面尝试-

select MAX(cast(substr(process,2,length(process)-1) as signed)) as max_process
from tablename
group by substr(process,1,1)

Try: 尝试:

select prefix, max(number) `max` from (
  select substring(process, 1, 1) prefix,
         cast(substring(process, 2, 100) as signed) number
  from MyTable
) a group by prefix;

Demo 演示

You get the 1st char of process with left(process, 1) and the numeric part with substring(process, 2) . 您使用left(process, 1)获得process的第一个字符,并使用substring(process, 2)获得数字部分。
Then by simply adding 0 to the numeric part you convert it to a number: 然后,只需在数字部分添加0即可将其转换为数字:

select
  left(process, 1) process1, 
  max(substring(process, 2) + 0) max_process
from tablename
group by process1

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

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