I'm getting weird results on a MySQL SELECT statement that uses ORDER BY my_column ASC.
It's ordering the results the way a "computer" would order them, instead of a human:
Item F: 241.565853
Item B: 25.310854
Item D: 25.397155
Item C: 260.252356
Item A: 27.7740
Item E: 271.774058
How do I get it to ORDER BY in the correct manner? My SELECT statement has a couple LEFT JOINS-- not sure if that would make a difference.
Any suggestions on how to correct this problem?
Something like this should do it:
ORDER BY ABS(my_column) ASC
The column you're ordering by is most likely a string-based (varchar, text, etc) datatype. You're seeing lexically-correct ordering for such a datatype. Change the column to use a numeric datatype, or (less-preferably, because why are you storing a numeric value as a string) cast the column to a numeric type and perform the sort on that cast.
Related
Struggling this the following sql statment.
The database is MySQL with score being DECIMAL(5,1)
Im looking to order by the average score within the column
...AVG(COALESCE(score,0)) AS scoreAvg ... ORDER BY scoreAvg DESC
but the results are not as expected, I have products with a high score below ones without a score (score is 0).
It lookings like score is being treated as a string, I have tried.
CAST(SCORE AS DECIMAL(5,1))
but with no luck.
Thank you for any help,
Regards
Try repeating the expression:
ORDER BY AVG(COALESCE(score, 0))
The first explanation that I think of is that scoreavg is already in a table and the ordering is using the column rather than the computed expression.
Your value might be in varchar(max)
So you want to convert the varchar to decimal or numeric and add
Order by avg(COALESCE(column name,0))
Or
Order by avg(coalesce(cast(column name as decimal (5,1)),0.0)
After stripping down my sql string, turns out my "image_path" column in TEXT format was causing the issue. Changed it to varchar(255).
I did'nt post it because I didnt think it was relevent.
anyway fixed now.
Must be a bug in MySQL
I am having column named rating in the mysql database table with multiple values from 1+,2+,................9+,10+,12+. when i am sorting this column with query
select * from tbl_app order by rating desc
I am getting 9+ as highest value, can any one tell me how to get 12+ as highest value
SELECT rating,SUBSTR(rating,1,LENGTH(rating)-1) FROM tbl_app ORDER BY CAST(SUBSTR(rating,1,LENGTH(rating)-1) as SIGNED) DESC;
if the last char is always a '+',the sql above will work.
what have you kept the datatype of the column rating ? If you have kept it varchar or text then this query will not work for sorting values as per descending order.
Probably the easiest thing to do in MySQL is cast those odd looking strings to numbers:
order by cast(rating as unsigned) desc
-- or less explicitly
order by rating + 0 desc
Both of those casts will stop trying to convert the string to a number when they hit the + so you'll get them sorted numerically.
Simply removing the plus signs from the strings will still leave you with strings and '10' < '2' is just as true for strings as '10+' < '2+'. That's actually your whole problem: you're storing numbers as decorated strings when you should be storing them as integers and adding the + decorations when you display them. You really should fix your schema to make sense instead of adding ugly hacks to work around your schema's strange ideas.
try this :
select convert(replace(rating,'+',' '),unsigned integer) as x from tab order by x desc
sql_fiddle_demo
I want to display my cnumber in ascending order that is 1111111,2222,33333,4444,etc from the sql statement below.
SELECT
*
FROM
BIBLEBOOK
INNER JOIN
CHAPTER ON (BIBLEBOOK.BIBLEBOOK_Id=CHAPTER.BIBLEBOOK_Id)
INNER JOIN
VERS ON(CHAPTER.CHAPTER_Id=VERS.CHAPTER_Id)
WHERE
BIBLEBOOK.bnumber='2'
ORDER BY
cnumber ASC;*
But when i ran the sql result is
111111...10101010....111111...12121212....131313....22222222
how can i display the result like 1..2..3..4..5..6..
You can force a numerical sorting when converting your text data type to a numeric one. Use an explicit or this implicit cast
ORDER BY cnumber * 1 ASC
Your didn't include any DDLs to describe your tables, but it seems as though your cnumber is defined as a VARCHAR column, and thus is sorted lexicographically.
If it indeed only contains NUMERIC data and is always used as such, it might be a good idea to alter the column's datatype.
If you cannot do that, you can always explicitly cast it in your query so it's sorting numerically.
SELECT *
FROM BIBLEBOOK
INNER JOIN CHAPTER ON (BIBLEBOOK.BIBLEBOOK_Id=CHAPTER.BIBLEBOOK_Id)
INNER JOIN VERS ON(CHAPTER.CHAPTER_Id=VERS.CHAPTER_Id)
WHERE BIBLEBOOK.bnumber='2'
ORDER BY CAST (cnumber AS NUMERIC) ASC;
Looks like your cnumber is set to be text instead of numeric. Try using CONVERT function to force the ordering happen using standard number comparison instead of text one:
ORDER BY
CONVERT(cnumber, INTEGER) ASC;*
But I would start from considering your column data type change. Why is it set to be text when it handles numeric data on the first place?
I have in my DB a column of varchar type.
Table name: Transcations
Column name :authprocess
I want to find min() value on the varchar column.
authprocess
A1D1
A1D3
A1D4
A1D1
A1B1
A1D5
......
i am using the command
select min(authprocess) from Trnascation
then it give the "0000" value
Please tell me the command in Mysql.
MIN() makes no real sense for a character string, your best bet is to do a string sort by ascending values and select the top one perhaps??
SELECT TOP 1 FROM table ORDER ASC;
MIN() takes the first entry after sorting. For text columns, the sorting order is ascii order, so for alpha-numeric data it's 0..9a..zA..Z
You must have an entry with the value 0000, which will of course be the minimum.
It sounds like you need to restrict your search to "valid" entries. Assuming entries are valid if they have form letter-digit-letter-digit, try this:
select min(authprocess)
from Transcations
where authprocess regexp '[A-Z]\d[A-Z]\d'
You can adjust the regex to suit whatever your needs are - eg perhaps it's A-digit-D-digit, which would match the regex 'A\dD\d'
I have the field receiptno, which is a varchar holding a numeric value, and want to sort this. Sorting as a string gives me the wrong ordering. I'd like to try sorting it as an integer. Is there anyway to converting to integer in the order by clause so I can sort by integer in the query itself.
You can use cast or convert to convert the field type:
... ORDER BY CAST(receiptno AS INTEGER) ASC
Edit sorry, fixed syntax
So, change the type of this column