Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL sort by name

Is ir possible to sort a column alphabetically but ignoring certain words like e.g 'The'

e.g.

A normal query would return

string 1
string 3
string 4
the string 2

I would like to return

string 1
the string 2
string 3
string 4

Is this possible?

EDIT Please note I am looking to replace multiple words like The, A, etc... Can this be done?

like image 212
Lizard Avatar asked Dec 06 '25 18:12

Lizard


1 Answers

You can try

SELECT id, text FROM table ORDER BY TRIM(REPLACE(LOWER(text), 'the ', ''))

but note that it will be very slow for large datasets as it has to recompute the new string for every row.

IMO you're better off with a separate column with an index on it.

For multiple stopwords just keep nesting REPLACE calls. :)

like image 178
Emil Ivanov Avatar answered Dec 08 '25 07:12

Emil Ivanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!