2015-02-10 40 views
0

我在mytable表中有一列lastname varchar(50)。按組中最後4個字符分組並顯示組

我想拆分組lastname最後 4個字母和數組。

這是怎麼回事?

剛剛添加: 這是正確的嗎?

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; 
+0

你的失敗嘗試在哪裏? – 2015-02-10 22:33:27

+0

我剛纔分享了它。仍在測試它是否正確。謝謝你推動我自己做 - 我認爲我沒有機會在不到一個小時的時間內完成它。 – Haradzieniec 2015-02-10 22:41:55

+1

'RIGHT'更適合你的情況。 – 2015-02-10 22:46:37

回答

1

不知道如果MySQL支持小組在此不規範使用列別名由:

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

或者,也許:

​​
相關問題