Why when i use order by seriq asc and have numbers like "10000" "100000" "97000"
script show me the results:
1: 10000
2: 100000
3: 97000
?
because they are stored in your column as strings
1- try to change the column seriq from VARCHAR/CHAR to INT.
2- You can use CAST() to convert from string to int. e.g. SELECT CAST('123' AS SIGNED);
Your seriq column must be a numeric column for values to be sorted numerically. If it's a text column, values will be sorted in alphabetical order as per their collation. For example:
CREATE TABLE Test
(
Foo int
);
INSERT INTO Test VALUES (10000);
INSERT INTO Test VALUES (100000);
INSERT INTO Test VALUES (97000);
select * from Test order by Foo asc;
Fiddle
The quick fix is to have MySQL convert the string to a numeric value in the ORDER BY clause.
Change your query to:
ORDER BY seriq+0 ASC
Note that MySQL will attempt to convert any string to a numeric value, reading characters up to the first "invalid" character, e.g. '123A' will be converted to a numeric value 123.
Changing the column type to int is the best solution as it will give you the best data storage and performance. However, if that is not an option, the following workaround is possible:
select * from foo order by seqid+0
This forces a type cast of the order by column to int, and the sort happens numerically.
Related
I was working with PHP and mysql a table was given and I was given a table to sort which had a enum field
enum('Pending','Acive','INACTIVE');
I did not understand how the table was sorted?
Enumeration Sorting
ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a'). The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values.
To prevent unexpected results when using the ORDER BY clause on an ENUM column, use one of these techniques:
Specify the ENUM list in alphabetic order.
Make sure that the column is sorted lexically rather than by index number by coding ORDER BY CAST(col AS CHAR) or ORDER BY CONCAT(col).
Reference: http://dev.mysql.com/doc/refman/5.6/en/enum.html#enum-sorting
You can sort like this in Sql
ORDER BY CASE status
WHEN 'Pending' THEN 1
WHEN 'Acive' THEN 3
ELSE 2
END
On this MySQL Query the ordering is sorting all results digit by digit. What I want is to simply return the smallest number first. Am I going about this wrong, or is it just the way PHP sorts?
MySQL:
$history = mysqli_query($con,"SELECT id,credits,timeadded FROM users ORDER by credits ASC);
Returned Result (Credits only):
1,13,14,16,2,24,29,3,31
Desired Result:
1,2,3,13,14,16,24,29,31
Your credits column must be a string. So MySQL use string comparison to order your results. And, when you compare, for example, 11 and 2 strings, 2 is 'bigger' than 11 (2 is a bigger char than 1).
You should to something like this :
SELECT id,credits,timeadded FROM users ORDER by CAST(credits AS UNSIGNED)
Then MySQL will make the comparison considering credits as a number : 11 will be bigger than 2.
BTW, if you only store numbers into this column, why it is VARCHAR type ?
You can change it with an ALTER TABLE statement :
ALTER TABLE users MODIFY credits INTEGER;
As mentioned by OlivierH
Your credits column must be a string. So MySQL use string comparison
to order your results.
Modify your table so that 'credtis' column will be INT or as needed but still a number so MySql will treat it as such and will return results as you want them (sorted numerically not by string comparison)
Here's the query
SELECT * FROM TABLE WHERE id = '7883c2c8e6' AND value > '45' DESC
But for some reason it is returning a row where the value for the field value is 8. I have checked and it has not preceding or following characters. The field is currently set as varchar. I was wondering if this could affect the outcome?
If I change the direction of the > to < this row does not show up, but it should be the other way round surely?
Because '8' is greater than '45' (in alphabetical order).
Cast your values to int:
AND convert( value, unsigned) > 45
The field is currently set as varchar
That's the issue; if you use a comparison operator on a text field, it will sort the results alphabetically, rather than numerically.
To get around it, set the field to be an integer instead.
SELECT * FROM TABLE
WHERE id = '7883c2c8e6' AND (value * 1) > 45
order by value DESC
i have table and column id, and the values are 1, 2 ,3,...,10,11,12,13.
how to query get max id from varchar type ? i had try
select MAX(id) from table
but the result is 9, Please help ??
Looks like the values are strings and it selecting the maximum string. You have to cast them to numbers first if you want them to sort numerically. You can use CONVERT to do this:
SELECT MAX(CONVERT(id, SIGNED)) FROM table
You can also use CAST:
SELECT MAX(CAST(id AS SIGNED)) FROM table
They do nearly the same thing except CONVERT has some additional options if you need them.
You can cast the varchar to an integer - something like
SELECT MAX(CONVERT(id, SIGNED)) FROM table
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert
SELECT MAX(id+0) FROM table will do the trick
I´m sorting by Price, look:
SELECT Price
FROM re2_listings
ORDER BY Price asc
Result:
1.200.000,00
1.500.000,00
200,00
3.000,00
...but the correct way is:
200,00
3.000,00
1.200.000,00
1.500.000,00
Understand? How to do this?
Obviously, VARCHAR is not the best datatype for the storage of numerical amounts. If you must use it, you need to CAST the price column to an appropriate numeric type in the SELECT and ORDER BY parts of your statement, or create a view containing the CAST and SELECT from the view.
The datatype to CAST to, and the syntax for CASTing are product-dependent.
$q1 = "select Price from re2_listings order by CAST(Price AS Float) asc";
first, it helps to know your SQL variant to answer your question.
If you still have the ability to control the schema, you are much better off using a numeric data type (e.g. int or float) for the column and formatting it as a price when it is displayed.
MySQL or SQL Server supports:
SELECT t.price
FROM (SELECT r.price
CAST(r.price AS DECIMAL(15,2)) 'dprice'
FROM RE2_LISTING r) t
ORDER BY t.dprice
I casted the varchar column to the appropriate data type, and used the alias dprice so I could reference it for sorting. If you are sorting in ascending fashion, you don't need to specify ASC if you don't want to - it's the default.