Jump to content

[SOLVED] SQL ORDER BY excluding 'The '


karldesign

Recommended Posts

Hi

 

I have a query that I would like to perform, but am struggling with producing it.

 

Say you have a list of titles (ie: The Spanish, English, The French, Belgians) and I want to ORDER BY title ASC, how do I order them, but ignore the 'The ' in the SQL query.

 

So the output order would be: Belgians, English, The French, The Spanish?

 

Thanks in advance for any help!

Link to comment
Share on other sites

maybe... just maybe... you make a field in the table named "FullTitle" and one name "ShortTitle" and in LongTitle you have "The" included but on short you have it removed? so then you order by ShortTitle but the output will be the LongTitle..

 

just an idea... i'm new to mysql, looking forward to see the answers from someone more experienced =)

Link to comment
Share on other sites

If they're ALL The (for example The English, The French etc) then the below *should* work. You will need to change 15 to the longest string you have.

 

For example The English (if that's your longest word) would be 5, 7. The first number is where you want the string starts, the second is the length to be extracted.

 

SELECT SUBSTRING(`yourfield`, 5, 7) + 0 AS nationality, tb.* FROM `yourtable` tb ORDER BY nationality ASC

 

Something like

 

$query = mysql_query("SELECT SUBSTRING(`yourfield`, 7, 15) + 0 AS nationality, tb.* FROM `yourtable` tb ORDER BY nationality DESC");
while ($result = mysql_fetch_array($query))
{
echo "$result[yourfield]<br />";
}

Link to comment
Share on other sites

Some may not have 'The '...

 

I have found this:

 

SELECT *, IF ( LEFT( vch_title, 4)='The ', SUBSTRING(vch_title FROM 4), vch_title ) AS title_sort FROM tbl_links WHERE int_category = '$i' ORDER BY title_sort;

 

The problem is, all the ones that start with 'The ' accumulate at the top before alphabetically ordering the reamaining fields...

Link to comment
Share on other sites

Some may not have 'The '...

 

I have found this:

 

SELECT *, IF ( LEFT( vch_title, 4)='The ', SUBSTRING(vch_title FROM 4), vch_title ) AS title_sort FROM tbl_links WHERE int_category = '$i' ORDER BY title_sort;

 

The problem is, all the ones that start with 'The ' accumulate at the top before alphabetically ordering the reamaining fields...

 

Try add ASC after title_sort

Link to comment
Share on other sites

The following code will ignore the string "The " in the orderering of records

 

SELECT your_title_field FROM your_table ORDER BY REPLACE(your_title_field,"The ","");

 

It will ignore "The " wherever it appears not just at the front of the title string so may have a few undesired effects but will certainly sort your example as you want it

Link to comment
Share on other sites

Some may not have 'The '...

 

I have found this:

 

SELECT *, IF ( LEFT( vch_title, 4)='The ', SUBSTRING(vch_title FROM 4), vch_title ) AS title_sort FROM tbl_links WHERE int_category = '$i' ORDER BY title_sort;

 

The problem is, all the ones that start with 'The ' accumulate at the top before alphabetically ordering the reamaining fields...

Not sure why this wouldn't work...

Link to comment
Share on other sites

That does work if the "FROM 4" is changed to ",5" (and then is better than my suggestion  ;) )

As below :

SELECT *, IF ( LEFT( vch_title, 4)='The ', SUBSTRING(vch_title,5), vch_title ) AS title_sort FROM tbl_links WHERE int_category = '$i' ORDER BY title_sort;

 

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.