how to count rows by first letter?

前端 未结 3 514
暗喜
暗喜 2021-01-14 17:21

a noob question here!

I wrote this query, but the \"group by\" is very stupid... so, how can I correct this?

SELECT
    COUNT(*) AS total,
    \'x\'         


        
3条回答
  •  星月不相逢
    2021-01-14 17:45

    This should perform as well as any other option -

    SELECT
        LEFT(name, 1) AS first_letter,
        COUNT(*) AS total
    FROM contents
    GROUP BY first_letter
    

    If you want to run this query for a single letter at a time you can add the WHERE clause and drop the GROUP BY -

    SELECT COUNT(*) AS total
    FROM contents
    WHERE name LIKE 'a%'
    

提交回复
热议问题