MySQL Sort Alphabetically but Ignore “The”

前端 未结 6 705
一整个雨季
一整个雨季 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:07

    if you are sure that you will NEVER EVER have a typo (and use lowercase instead of uppercase)

    select *
    from books b 
    order by UPPER(LTRIM(Replace(b.Title, 'The', '')))
    

    Otherwise your sorting will do all Upper and then all lower.

    for example, this is ascending order:

    Have a Great Day
    Wild west
    Zorro
    aZtec fries are hotter
    alfred goes shopping
    bart is small
    will i am not
    

    adapted from AJP's answer

提交回复
热议问题