karldesign Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/ Share on other sites More sharing options...
Gibbs Posted April 12, 2007 Share Posted April 12, 2007 I don't fully understand them in MySQL but if you don't get any useful replies (which is doubtful ) look up using SUBSTRING. Do they ALL have "The"? If so it would probably be easier... Link to comment https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227457 Share on other sites More sharing options...
clown[NOR] Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227460 Share on other sites More sharing options...
karldesign Posted April 12, 2007 Author Share Posted April 12, 2007 maybe... just maybe... you make a field in the table named "FullTitle" and one name "ShortTitle" This could be good... I'm just thinking its a bit long winded for something that, effectively, should be simple... I think?! Link to comment https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227470 Share on other sites More sharing options...
Gibbs Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227474 Share on other sites More sharing options...
karldesign Posted April 12, 2007 Author Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227481 Share on other sites More sharing options...
Gibbs Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227484 Share on other sites More sharing options...
karldesign Posted April 12, 2007 Author Share Posted April 12, 2007 Still no joy - the 'The ' still come first... Link to comment https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227486 Share on other sites More sharing options...
Tyche Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227512 Share on other sites More sharing options...
fenway Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227518 Share on other sites More sharing options...
Tyche Posted April 12, 2007 Share Posted April 12, 2007 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 https://forums.phpfreaks.com/topic/46687-solved-sql-order-by-excluding-the/#findComment-227527 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.