Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/162812-query/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/162812-query/#findComment-859149
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/162812-query/#findComment-859150
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/162812-query/#findComment-859151
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.