I am having issues comparing two values in the Mysql database and display the lowest one.
For instance I have this:
value1 = 23.4
value2 = 4.479
I tried this:
ORDER BY CAST(column AS SIGNED) ASC
but its not working
Again, field type is VARCHAR.
The result I am looking for should be 23.4 and not 4.479
any suggestions?
Simply put, you need to change your column to DECIMAL if you're storing prices. VARCHAR will be in alphabetical order. Read up on Numeric Types in MySQL.
ALTER TABLE your_table MODIFY COLUMN price DECIMAL(10,3);
A pretty costly work-around for you is to cast all values to decimal before sorting:
SELECT name, price FROM test ORDER BY CAST(price AS DECIMAL(20, 10)) ASC LIMIT 1
Just make sure your DECIMAL limits are enough to correctly cast all values in the column.
It will work if you CAST it to DECIMAL.
select * from tablename order by cast(price as decimal);
+--------+-------+
| name | price |
+--------+-------+
| nail | 0.95 |
| glue | 9.23 |
| hammer | 45.12 |
+--------+-------+
Related
I have got SQL table where I have column DISTANCE (varchar50). It's number with dot.
+----+--------+--------+----------+
| ID | USERID | MONTH | DISTANCE |
+----+--------+--------+----------+
| 1 | 1 | 201707 | 7.25 |
+----+--------+--------+----------+
| 2 | 2 | 201707 | 9.17 |
+----+--------+--------+----------+
| 3 | 2 | 201707 | 10.31 |
+----+--------+--------+----------+
| 4 | 1 | 201706 | 10.08 |
+----+--------+--------+----------+
I would like to display MAX value od DISTANCE. If I ORDER BY distance DESC the result is:
SELECT * FROM mytable ORDER BY distance DESC
9.17
7.25
10.31
10.08
I try to find the MAX but still is not correct
SELECT MAX(distance) AS mvzd FROM mytable
9.17
How I can have correct max distance value which should be 10.31 with the provided sample data ?
PROBLEM
SOLUTION
You can use the following query :
select max(cast(distance as decimal(10,2))) AS mvzd FROM mytable
By your existing query, you are finding MAX on a VARCHAR typed column and you are getting correct result what you wrote! If you want to have your desired result, first you have to convert the type of that column like i did in the above query.
The problem is that you use wrong data type of the DISTANCE. In your case you store the distance values as varchar and that's why the ORDER BY and MAX() functions does not work as expected - in this case the ORDER BY is sorting values as a string from the very first character. In your example you tried to sorted in descending order to be like from lowest to larger - this will give you "unexpected" results if there will be value like 98.76 or 111.15.
All you need is to set the data type of DISTANCE column to float - a small number with a floating decimal point.
Change settings of your table to use float:
ALTER TABLE `mytable`
CHANGE COLUMN `distance` `distance`
FLOAT NULL DEFAULT NULL AFTER `month`;
Data stored as float will support all native functions such as MIN, MAX, ORDER BY etc. which will work as expected without any workarounds.
Here is corrected example using float: http://sqlfiddle.com/#!9/1f8692/1
Here is extended example using incorrect varchar(50): http://sqlfiddle.com/#!9/4da754e/1
How can I sort by multiple columns in SQL and in different directions. column1 would be sorted descending, and column2 ascending.
ORDER BY column1 DESC, column2
This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two or more rows are equal.
The other answers lack a concrete example, so here it goes:
Given the following People table:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
Thomas | More | 1478
Thomas | Jefferson | 1826
If you execute the query below:
SELECT * FROM People ORDER BY FirstName DESC, YearOfBirth ASC
The result set will look like this:
FirstName | LastName | YearOfBirth
----------------------------------------
Thomas | More | 1478
Thomas | Jefferson | 1826
Thomas | Alva Edison | 1847
Benjamin | Franklin | 1706
SELECT *
FROM mytable
ORDER BY
column1 DESC, column2 ASC
Multiple column ordering depends on both column's corresponding values:
Here is my table example where are two columns named with Alphabets and Numbers and the values in these two columns are asc and desc orders.
Now I perform Order By in these two columns by executing below command:
Now again I insert new values in these two columns, where Alphabet value in ASC order:
and the columns in Example table look like this.
Now again perform the same operation:
You can see the values in the first column are in desc order but second column is not in ASC order.
You can use multiple ordering on multiple condition,
ORDER BY
(CASE
WHEN #AlphabetBy = 2 THEN [Drug Name]
END) ASC,
CASE
WHEN #TopBy = 1 THEN [Rx Count]
WHEN #TopBy = 2 THEN [Cost]
WHEN #TopBy = 3 THEN [Revenue]
END DESC
SELECT id,
first_name,
last_name,
salary
FROM employee
ORDER BY salary DESC, last_name;
If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY. This clause comes at the end of your SQL query.
After the ORDER BY keyword, add the name of the column by which you’d like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name). You can modify the sorting order (ascending or descending) separately for each column. If you want to use ascending (low to high) order, you can use the ASC keyword; this keyword is optional, though, as that is the default order when none is specified. If you want to use descending order, put the DESC keyword after the appropriate column (in the example, we used descending order for the salary column).
SELECT * FROM EMP ORDER BY DEPTNO ASC, JOB DESC;
TRY
'select * FROM users ORDER BY id DESC, name ASC, age DESC
You can also sort or order by the Number of Characters in each Column you wish to sort by. Shown below is a sample which sorts by the first three characters of the First Name and by the last two characters in the name of the town.
SELECT *
FROM table_name
ORDER BY LEFT(FirstName, 3) ASC, LEFT(Town, 2);
we are working with a table that has a column for every SKU ordered by a customer. So if a particular SKU has been ordered by five different customers, it will appear 5 times. Also, some customers may order 2 of that SKU so in the 'number_ordered' column next to it, there will be a 2. I'm not so good at drawing this out in words so I'll give an example of what the database looks like.
+------------+-----------------+
| item_SKU | Number Ordered |
+------------+-----------------+
| SKU001 | 3 |
| SKU001 | 2 |
| SKU002 | 15 |
| SKU003 | 1 |
+------------+-----------------+
How can I times the sku by the number ordered and then add them all together in MySQL. I need to put it into PHP but I can do that if i get some hints on how to do this.
Cheers
Use this to get the total ordered item of same item_SKU and use it in PHP. If you need the order count also, use COUNT() function
SELECT item_SKU, SUM(Number_Ordered) as Total_Ordered_Item, COUNT(Number_Ordered) as Total_Order_Count
FROM table
GROUP BY item_SKU
Try using count' with group by sku name (SKU001). it returns an output with the count of different sku's.
try below SQL query
1.Sum of Orders
select item_SKU,sum(number_ordered) as orders from `table Name` group by item_SKU order by item_SKU
output
item_SKU | Number Ordered
SKU001 | 5
SKU002 | 15
SKU003 | 1
2. Query to count no of orders
select item_SKU,count(number_ordered) as orders from `table Name` group by item_SKU order by item_SKU
output
item_SKU | Number Ordered
SKU001 | 2
SKU002 | 1
SKU003 | 1
Try this query:
SELECT item_SKU,
COUNT(item_SKU) CNT,SUM(Number_Ordered) TOTAL
FROM TABLE1 GROUP BY item_SKU;
SQL Fiddles
Select item_SKU, SUM(NumberOrdered) As total from [table name] group by item_SKU
Try this one. It gives you total no of occurrence as 'COUNT' and sum of number_ordered as 'SUM_NUM_ORDER'
SELECT item_SKU,COUNT(item_SKU) as COUNT,SUM(Number_Ordered) as SUM_NUM_ORDER FROM TABLE1 GROUP BY item_SKU;
DEMO: http://sqlfiddle.com/#!2/9ec2b9/10
I need to compare numbers stored in varchar fields, for example i have a table:
id | values
1 | 2
2| 154
3 | 88
4 | 35
and I need to look for numbers, that are higher than 5, if I could use int attribute for values fields everything would be ok, but I have to use varchar. Is there any simple solution?
you need to use
CAST(`values` AS UNSIGNED)
like that
select * from table WHERE CAST(`values` AS UNSIGNED) > 5
DEMO
EDIT:
lets say you have signed number with negative sign -
then
select * from table1 WHERE CAST(`values` AS SIGNED) > 5
DEMO
How do you order by number if your number can fall below and be bigger than 0?
Example Mysql table:
|Name|Karma|
__________
|Me | -8 |
|Bill| 5 |
|Tom | 2 |
|Saly| 0 |
|San.| -3 |
Example select query
$sql="SELECT karma FROM table ORDER BY karma DESC";
The result I get is this (separated by comma): 5,2,0,-8,-3.
Shouldn't it be 5,2,0,-3,-8?
I discovered somewhere in the internet that mysql orders by string. How to make it order by number?
Karma will be ordered as a string if you have made it a string, i.e. a varchar column.
Convert the column to a INT, and it will order numerically.
You also have the option of not changing the table, but casting the column into the right type while sorting:
SELECT karma FROM table ORDER BY CAST(karma AS int) DESC
but that is bad for performance.
There's another weirdest option:
SELECT karma FROM table ORDER BY karma+0 DESC