ainoy31 Posted June 18, 2009 Share Posted June 18, 2009 Does anyone know why this query is not ordering the list via descend? $sql = "SELECT sessionid, legal_name, trade_name, length(trade_name) AS length, physical_state AS state, physical_city AS city, entity_county AS county FROM table.form WHERE sessionid='$sessionid' ORDER BY length DESC"; Thanks. AM Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/ Share on other sites More sharing options...
Maq Posted June 18, 2009 Share Posted June 18, 2009 What is the actual order? What type is length? Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-859141 Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2009 Share Posted June 18, 2009 length(trade_name) AS length would order the results by the length of the string in trade_name. What results are you getting and what results do you expect? Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-859142 Share on other sites More sharing options...
ainoy31 Posted June 18, 2009 Author Share Posted June 18, 2009 PFMaBiSmAd - I want it to order by the length of the string but it is not doing that. Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-859147 Share on other sites More sharing options...
PFMaBiSmAd Posted June 18, 2009 Share Posted June 18, 2009 Cannot help you until you show what you are getting, because we only see the information in your post and so far you have not provided any specific information to go by. Have you checked if your data in the table contains any non-printing/white-space characters? Edit: and assuming that you have read the documentation for what you are doing - LENGTH(str) Returns the length of the string str, measured in bytes. A multi-byte character counts as multiple bytes. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5. Are you using any multi-byte character encoding? Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-859149 Share on other sites More sharing options...
akitchin Posted June 18, 2009 Share Posted June 18, 2009 from the mysql manual, the LENGTH() function returns the string's length in bytes, not in characters: LENGTH() in MySQL manual try using CHAR_LENGTH() and see if that fixes your issue. otherwise try actually echoing length in each iteration to see what it contains. Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-859150 Share on other sites More sharing options...
o3d Posted June 18, 2009 Share Posted June 18, 2009 try "ORDER BY length(trade_name) DESC" instead of "ORDER BY length" But the example below works for me: select length(column1) as length, column1 from ( select 'aaaaaa' as column1 union select 'aaa' as column1 ) as a order by length DESC Returns: 6 aaaaaa 3 aaa Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-859151 Share on other sites More sharing options...
fenway Posted June 22, 2009 Share Posted June 22, 2009 you should probably use char_length(), so that your code works if you ever switch to a multi-byte collation. Quote Link to comment https://forums.phpfreaks.com/topic/162812-query/#findComment-860980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.