在语句中使用 DISTINCT、COUNT 和 SUBSTRING

Using DISTINCT, COUNT AND SUBSTRING in a statement

我下面有一个 id

我想 count 列中从右侧 3 开始的 3 个唯一字符。所以我这样做了:SELECT SUBSTRING(id,3,3) FROM lhr 我得到了这个结果:

现在如何计算 id 列中的总数,例如 A00 = 7 和 A90 = 1?我尝试 Count As 这样的:SELECT SUBSTRING(id,3,3), COUNT(*) AS 'total' FROM lhr

但是没有用。我没有得到我想要的结果。我得到的结果是 total = 8。请帮助我并提前致谢。

select mydata, count(*) from 
(SELECT SUBSTRING(id,3,3) as mydata FROM lhr) a
group by mydata

这应该适合你。

更好的方法:

select substring(id,3,3), count(*) 
from 1hr 
group by substring(id,3,3)