Returning the DISTINCT first character of a field (MySQL)

后端 未结 3 1523
刺人心
刺人心 2020-12-24 02:47

I would like to produce a character list of all of the first letters of column in my database. The SQL below illistrats what I would like to return.

SELECT DISTIN         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 03:21

    Sorry to do this, but I figured out exactly what I needed to do just now.

    SELECT DISTINCT LEFT(name, 1) FROM mydatabase
    

    This returned a list of the first, distinct, single characters that each row in the column started with. I added changed it to the following to get it the list in alpha-numeric order:

    SELECT DISTINCT LEFT(name, 1) as letter FROM mydatabase ORDER BY letter
    

    Works like a charm.

提交回复
热议问题