mysql select in aescending or descending order - mysql

my id or primary key is and the data type of is VARCHAR(50)
0.0.01
0.0.100
0.0.101
0.0.1011
0.0.201
0.0.501
0.0.99
0.0.999
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
100.100.100
25.45.1001
99.99.99
I have to get it in sorted order
so i tried this
select id from table order by cast(id as decimal) desc;
but it does not work
the expected order is after running the query
0.0.01
0.0.99
0.0.100
0.0.101
0.0.201
0.0.501
0.0.999
0.0.1011
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
25.45.1001
99.99.99
100.100.100
i am using mysql for this

Not an easier one but you can use substring_index for each decimal places
select *
from t
order by
substring_index(id,'.',1) * 1,
substring_index(substring_index(id,'.',-2),'.',1) * 1,
substring_index(id,'.',-1) * 1
Explanation
I have use substring_index what it does it will return the piece of string in provided column like in above case i have use id column by the occurrence of delimiter i.e(.) for example a string like 0.1.2 for above 3 sunstring_index usage will return as below
substring_index('0.1.2','.',1) will give result as 0
substring_index(substring_index('0.1.2','.',-2),'.',1) will give result as 1
substring_index('0.1.2','.',-1) will give result as 2
For type casting to number i have multiplied the result of substring_index to 1 so the order by expression will first order the results by the number before first dot then with number before second dot and last the number after second dot in ascending manner
Demo
Sources:
http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php

Your ID column has invalid decimal value so casting could not work here.
Try without casting this should work:
select id from table order by id desc;
DEMO

Related

getting maximum value of a varchar column in database

how can i get the maximum value of my column that is a varchar with these values
for example i have a fieldname of myid which is varchar and what i want is to get the maximum value of the myid field . How can i query to get the 1-10 value of myid column?
myid
1-1
1-2
1-3
1-4
1-5
1-6
1-7
1-8
1-10
1-9
I would suggest using this trick:
order by length(myid) desc, myid desc
This will work for the data in the question. A more general answer is:
order by substring_index(myid, '-', 1) + 0, substring_index(myid, '-', -1) + 0
For the data you've shown:
select myid
from data_table
order by cast(substr(myid, 3, 2) as int) desc
limit 1;
In this case the ordering function is the integer value of the portion of the identifier following the dash. In general--i.e., for different or more complex data--you simply need to determine what the appropriate odering function is.

MYSQL multiply column value by value in previous row

I hope I can phrase this properly - I'm not sure I've been approaching this correctly.
I am running a MySQLi query for which I need to order the results by the result of a sum (multiplicaiton) of the values in one column. The values are to 2 decimal places and are 'odds' for sporting results. As Such i can not simply sum the values from each row as the result (e.g. 1,1,1 adds to 3 but multiplies to 1) does not give me 'correct' ordering.
At present i am simply performing a sum in my query
SUM(Fav_odds) AS Total
But i'm stumped how i can get 'Total' to be the result of 'Fav_odds * number of rows' in my query.
Fav_odds | Vendor
------------------
1.2 | Name
2.1 | Name
3.2 | Name
So for Vendor called 'Name' i would like to give a multipled value for items in Fav_odds column (e.g. 1.2 * 2.1 * 3.2 = 8.064)
select round(EXP(SUM(LOG(fav_odds))),3) as fav_odds from table;
use a variable
set #miller := 1;
select orderingTotal from
(select #miller:=#miller*Fav_odds as orderingTotal
from mytable) mytotaltable
order by orderingTotal;
or it could be a matter of just saying
sum(Fav_odds) * count(Fav_odds)
if what you are saying at the end is the right way ("fav_total * the number of rows").

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

I want to get the maximum value from my S_ID column which is declared as varchar type

My S_ID values are
S_1
S_2
S_3,...., S_11.
I use SELECT MAX(S_ID) FROM stock_detail to get the maximum value.
It is working till S_9 but when it reaches S_11: that query only give me S_9 as the maximum value. How can I do to get S_11 as the maximum value ? Please help me, I am just a beginner in programming.
You can get the maximum value by using this construct:
select s_id
from stock_detail
order by length(s_id) desc, s_id desc
limit 1;
This puts the longer values first.
If you want to use max(), then you need to deconstruct the number. Something like:
select concat('S_', max(replace(s_id, 'S_', '') + 0))
from stock_detail;
This allows you to get a numeric maximum value rather than a character maximum value, which is the root of your problem.

how to sort varchar numeric columns by DESC or ASC?

I write ...
ORDER BY column ASC
but my column is VARCHAR and it sorts wrong like 1, 10, 2, instead of 1, 2, 10.
How can I do it to sort like 1, 2, 10?
order by
cast(column as float)
Notes:
Assumed you only have numbers in the columns. No "fish" or "bicycle"
empty strings CAST to zero
Edit: For MySQL. You can not cast to float
order by
cast(column as decimal(38,10))
You can cast to int...
order by cast(column as int)
DEMO
DECLARE #q as table(
name varchar(50),
columnn varchar(10)
)
insert into #q
VALUES('one','1'),('one','10'),('one','20'),('one','3'),('one','2'),('one','20')
select * from #q order by cast (columnn as int) desc
prints
-------------------------------------------------- ----------
one 20
one 20
one 10
one 3
one 2
one 1
So, Daniel, yes, it works :)
UPDATE:
order by cast(column as decimal(20,6))
Will cast the column values to decimal numbers with 20 digits max and 6 decimal places. Adjust it to your actual requirements.
Try this:
order by CAST(column as UNSIGNED)
i used this way
multiply it with one the query is :
ORDER BY columnname * 1 ASC
Example: Table user have value with column value [varchar(20)].
Then you can query it:
SELECT * FROM user ORDER BY value * 1
After we multiply it MySQL will treat it like a number but this way is not recommended for a heavy load.