按最后 4 个字符分组并显示该组

split in groups by last 4 characters and show that groups

我在 mytable table.

中有一列 lastname varchar(50)

我想按 last 4 个字母拆分组上的 lastname 列并计算这些组。

有什么方法可以做到这一点?

刚刚补充: 这是正确的吗?

select substr(lastname,-4) as alpha, count(lastname)
  from mytable
 group by substr(lastname,-4)

新更新:

select right(lastname,4) as alpha, count(lastname) as total
  from orig
 group by alpha
 order by total desc;

不知道 MySQL 是否支持在组中使用这种非标准的列别名:

select right(lastname,4) as alpha,
       count(lastname) as total
from orig
group by alpha
order by total desc;

或者,也许:

select alpha, count(*) as total
from (select right(lastname,4) as alpha from orig)
group by alpha
order by total desc;