MySQL Sort Alphabetically but Ignore “The”

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

    0 讨论(0)
  • 2020-12-16 19:10
    select *
    from books b 
    order by LTRIM(Replace(b.Title, 'The', ''))
    

    PLease note this will replace The from the title.. no matter where in the title. so use substring to get first 3 characters.

    0 讨论(0)
  • 2020-12-16 19:12

    do a case when to check if the column value starts with the and if it does, return the title without the 'The'. This will be a new column that you will be using later on for the sort order

    select title, case when title like 'The %' then trim(substr(title from 4)) else title end as title2 from tablename order by title2;
    
    0 讨论(0)
  • 2020-12-16 19:12

    I've seen some convoluted answers here which I tried but were just wrong (didn't work) or unsafe (replaced every occurrence of 'the'). The solution I believe to be easy, or maybe I'm getting it wrong or not considering edge cases (sincerely, no sarcasm intended).

    ... ORDER BY SUBSTRING(UPPER(fieldname), IF(fieldname LIKE 'The %', 5, 1))
    

    As stated elsewhere, UPPER just prevents ASCII sorting which orders B before a (note the case difference).

    There's no need for a switch-case statement when there is only one condition, IF() will do

    I'm using MySQL 5.6 and it seems like the string functions are 1-indexed, in contrast to say PHP where strings are 0-indexed (this caught me out). I've tested the above on my dataset and it works

    0 讨论(0)
  • 2020-12-16 19:16

    You can use a CASE statement in the ORDER BY and the use REGEXP or LIKE to match strings that start with words you would like to remove.

    In the example below I find all words that begin with a, an, or the followed by a space, and then remove everything up to the space, and trim away additional white space (you might have two or spaces following an instance of the).

    SELECT *
    FROM books
    ORDER BY 
      CASE 
        WHEN title REGEXP '^(A|An|The)[[:space:]]' = 1 THEN 
          TRIM(SUBSTR(title , INSTR(title ,' '))) 
        ELSE title
      END ;
    
    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题