views:

211

answers:

4
+2  Q: 

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?

A: 

Yes, it should be possible to use expressions with the ORDER-part:

SELECT * FROM yourTable ORDER BY REPLACE(yourField, "the ", "")
Bobby
Might want to add a trailing space, otherwise this might cause some problems with words containing "the", eg. feather
astander
@astander: Good idea, thanks.
Bobby
A: 

This will replace all leading "The " as an example

SELECT  *
FROM    YourTable 
ORDER BY REPLACE(Val,'The ', '')
astander
+3  A: 

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. :)

Emil Ivanov
A: 

I have a music listing that is well over 75,000 records and I had encountered a similar situation. I wrote a PHP script that checked for all string that began with 'A ', 'An ' or 'The ' and truncated that part off the string. I also converted all uppercase letters to lowercase and stored that string in a new column. After setting an index on that column, I was done.

Obviously you display the initial column but sort by the newly-created indexed column. I get results in a second or so now.

Ronald D. Willis