Jump to content

Order by "The"


PHP Monkeh

Recommended Posts

I don't usually post new topics but this one has me stumped :D  Although the subject isn't that descriptive I couldn't think of what to call it.

 

I'm creating a catalogue, and as such it requires to have alphabetical ordering.  Now I can select all titles beginning with A, B, C etc. and even the ones that start as "The A", "The B" and so on.  The problem is the ordering, all of the titles beginning with "The A" go to the end because of the T.  Is there any way to order by the A value rather than the The.  At the moment it's like this:

 

Almighty DVD Title
Another DVD Title
The Almost Perfect DVD Title

 

Whereas I want it to be like this

 

Almighty DVD Title
The Almost Perfect DVD Title
Another DVD Title

 

Hopefully you can see what I'm trying to achieve.  I assume it's something to do with SUBSTR() or maybe there's some REGEX I'm not aware of, but hopefully someone out theres done this :)  Cheers.

Link to comment
Share on other sites

OK I've just noticed why it worked once I took the ORDER BY out.  The system I used to generate the input (it's from a report) was automatically putting them in to the correct order, so my MySQL statement was ordering them by ID, which would be correct in this case (as they were in the right order from the report).

 

So I guess the question still stands, how can you order this way?

Link to comment
Share on other sites

OK I've figured out how to order by the second word, I used REPLACE() in my SQL statement.  This is my current SQL statement for anyone interested:

 

$query = sprintf("SELECT id, title, REPLACE(title, 'The ', '') AS Title FROM `titles` ORDER BY Title ASC  LIMIT %s, 10", mysql_real_escape_string($limit, $db->dbh));

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.