MySQL Sort Alphabetically but Ignore “The”

前端 未结 6 706
一整个雨季
一整个雨季 2020-12-16 18:22

I have MySQL database that has a table with book data in it. One of the columns in the table is called \"title\". Some of the titles begin the word \"the\" and some do not.<

6条回答
  •  伪装坚强ぢ
    2020-12-16 19:21

    Simply:

    SELECT Title
    FROM book
    ORDER BY IF(Title LIKE "The %", substr(Title, 5), Title);
    

    Explanation:

    We use the IF function to strip the "The" (if present) from the beginning of the string before returning the string to the ORDER BY clause. For more complex alphabetization rules we could create a user-defined function and place that in the ORDER BY clause instead. Then you would have ...ORDER BY MyFunction(Title).

提交回复
热议问题