I am using simple order by clause to show products according to available quantity
Below is the query I am using:
SELECT * FROM productsinfo ORDER BY quantity desc
Query giving no error but sorting order is not correct.Anyone please tell me where I am wrong.
EDIT
Have checked my quentity clumn is varchar type.I am storing values in 1,215 10,456 format.
Might be your quantity column is varchar type so it's not sorting as numbers.Please check.
You need to cast it in integer type
Try below:
SELECT * FROM productsinfo ORDER BY CAST(quantity AS UNSIGNED ) desc
OR Use below trick.
SELECT * FROM productsinfo ORDER BY quantity+0 DESC
I think you define quantity as VarChar. Because if it's a Number (int, smallint, decimal,..) the order will be definitely correct.
SELECT *, CAST(quantity AS int) QuantityA
FROM productsinfo
ORDER BY QuantityA desc
You'll have to first remove the , from the value and turn the result into a number. Give this a try:
SELECT * FROM productsinfo
ORDER BY REPLACE(quantity, ',', '')+0 DESC
The exect answer is:-
for varchar data type it compares integert data from left to right that means it treat 100 less the the 11.
So thats why comparing and sorting on varchar data type for integer data is a bad choice.
Convert it to int using cast in a query or alter your table.
Related
I'm trying to solve an issue. When I write a query
SELECT ID, STOCK_ID, FILE_NAME FROM motor_images where STOCK_ID = 25
It does not sort my record. It shows random order. I want to sort it as per ID. I've also tried ORDER BY keyword but it does not work. Please help.
Query Result
if your id isn't an auto generated int; it will sort by strings meaning 1,2,3...123, 124,125 etc will be ordered as
1,123,124,125,2,3 etc.
what you need to do in this case is use a cast by int
SELECT id, stock_id, file_name FROM motor_images WHERE stock_id = 25 ORDER BY CAST(id as UNSIGNED)
Keep in mind the UNSIGNED is an integer; read more in the mysql documentation on casting. I could be wrong but there's no other reason I could think of that would cause the ids not to order correctly. http://ftp.nchu.edu.tw/MySQL/doc/refman/5.0/en/cast-functions.html#function_cast
I am fighting for hours trying to figure out how can i sort the Column PRICE from Cheaper to More expensive.
I use decimal(5,2)
Values are like this:
20.99
15.99
10.15
9.20
i use CONCAT to insert the $ infront of each value. The result is like this
$10.15
$15.99
$20.99
$9.20
Desire result is this :
$9.20
$10.15
$15.99
$20.99
Use Order by Price DESC where DESC is descending order and if you want ascending use ASC
For sorting you have to use ORDER BY clause.
For sorting from low to high try this:
SELECT CONCAT("$",CAST(price AS CHAR)) AS currency
FROM tablename
ORDER BY currency
Order by price
Do not order by concat, because it will then order by the alphanumeric values instead of numeric.
Your price column must have a character CHAR() or VARCHAR() type rather than a numeric type. Cast it as a DECIMAL in the ORDER BY:
Select * From table ORDER BY CAST(price AS DECIMAL(10,2)) DESC
The real fix for this would be to change the price data type to a proper numeric type.
I have this sql query:
SELECT * FROM `Fac__Invoice` ORDER BY `Fac__Invoice`.`invoice_number` ASC
The result is this table:
The problem is that number 10 is after 1. It should be after 9. This is because invoice_number is a varchar. I deliberately choose varchar because i want to store numbers like for example "FA001".
Can you write a sql query so that invoice_number 10 will be placed underneath 9?
Or is there any other solution?
Try to caste the value to unsigned:
SELECT * FROM `Fac__Invoice` ORDER BY CAST(`Fac__Invoice`.`invoice_number` AS UNSIGNED) ASC
i have one table trip_data.Every one second i getting packets and inserting data to database.trip_data table contains four fields.trip_paramid,fuel_content,creation_time&vehicle_id.I want to select all rows in which difference between creation time is 2 minutes(Not exactly 2.Approximately 2).trip_data table contains 40 lacks rows.So i need a optimized select query for this.Can anyone help on this.Here is table schema&sample data for the trip_table..
SQlFiddle demo
SELECT
tp.*
FROM
trip_parameters tp
GROUP BY
CONVERT(UNIX_TIMESTAMP (tp.creation_time)/(2*60), unsigned)
ORDER BY
tp.creation_time asc
Note that using UNIX_TIMESTAMP does not allow you to handle dates beyond year 2037. Using the following instead fixes the problem:
CONVERT(TIMESTAMPDIFF(SECOND,'1970-01-01 00:00:00',tp.creation_time)/(2*60), unsigned)
You can do it in one table scan using MYSQL User defined variables. Unfortunately UDV's have a limited set of data types (integer, decimal, floating-point, binary or nonbinary string). So in this query I use a char #ti varible to store previous datetime using CAST to compare it with the Creation_time field. Also initial value for this variable I set to (now()-10000000) you can use any date you wish less than MIN(Creation_time)
Here is the SQLFiddle demo
select * from
(
select trip_parameters.*,
if(ABS(TIMESTAMPDIFF(MINUTE,Creation_time,cast(#ti as datetime)))>=2,1,0) t,
#ti:=if(ABS(TIMESTAMPDIFF(MINUTE,Creation_time,cast(#ti as datetime)))>=2,
cast(Creation_time as char(100)),#ti)
from trip_parameters,
(select #ti:=cast(now()-10000000 as char(100))) a
order by creation_time
) t2
where T=1
order by creation_time
Try this
SELECT trip_paramid, fuel_content, creation_time, vehicle_id
FROM trip_parameters
GROUP BY FLOOR(UNIX_TIMESTAMP(creation_time)/120)
This takes one item of every 2 minute block
I have a mysql database with a field "order_number" set as an INT on the odd occasion the order number would need to have a trailing r eg 2100r obviously INT will only accept numbers and would sort the number correctly ASC or DESC if I use VARCHAR to overcome this restriction it will correctly accept the trailing r character but will not sort the numbers in numerical order correctly, is there a way INT option can be forced to accept a character?
You need to store your account ids as character strings if any of them contain letters. But you can order them correctly as long as the letters are always suffixes. This works whether or not your AccountID values have leading spaces.
SELECT *
FROM Account
ORDER BY CAST(AccountID as UNSIGNED INTEGER), AccountID
This will order numerically, and then deal with any equal numbers by ordering lexically.
http://sqlfiddle.com/#!2/a16bf/8/0
If you wanted the "r" orders to be shown before their unadorned friends with the same number, you could do this:
SELECT *
FROM Account
ORDER BY CAST(AccountID as UNSIGNED INTEGER), AccountID DESC
you can use CAST function in MySQL but it's a good practice to store order_number as int using typecasting before inserting new data in table.
SELECT CAST(int_col AS CHAR);
SELECT CAST(char_col AS unsigned INTEGER);
No, there is no such option.
You should probably use a VARCHAR column for the order number and a separate column (possibly INT) just for sorting; populate the helper column based on what the order number is when inserting and updating rows.
Try to convert string to number using this way (number_string * 1), e.g. -
SELECT * FROM table ORDER BY column * 1;
No , Not possible. As #Jon said, there is no other options to force an INT field to keep VARCHAR data.