MySQL and PHP using rank by ASC - mysql

Trying to fix ranking off scores table, I put this in;
SELECT * from scores where tournament_id = "3" AND class = "MA1" ORDER BY Rank ASC
It came out
1
11
12
13
2
22
3
31
4
5
6 and on, how can it be fixed?
Thanks

Your rank column data type is string. So it is returning as above please convert it in number or int format.
SELECT CAST("10" as SIGNED) ;

Seems like your rank column is a textual column (e.g., varchar), and therefor, when you order by it, you order lexicographically. If you want to sort it numerically, you need to explicitly convert it:
SELECT *
FROM scores
WHERE tournament_id = "3" AND class = "MA1"
ORDER BY CAST(rank AS INTEGER) ASC
-- Here -^

SELECT * from scores
where tournament_id = "3"
AND class = "MA1"
ORDER BY CAST(Rank AS INTEGER) ASC
You should casting the type char to int your rank field for order by

Related

SQL SELECT ORDER BY multiple columns depending on value of other column

I have a table with the following columns:
id | revisit (bool) | FL (decimal) | FR (decimal) | RL (decimal) | RR (decimal) | date
I need to write a SELECT statement that will ORDER BY on multiple columns, depending on the value of the 'revisit' field.
ORDER BY 'revisit' DESC - records with this field having the value 1 will be first, and 0 will be after
If 'revisit' = 1 order by the lowest value that exists in FL, FR, RL and RR. So if record 1 has values 4.6, 4.6, 3.0, 5.0 in these fields, and record 2 has values 4.0, 3.1, 3.9, and 2.8 then record 2 will be returned first as it holds a lowest value within these four columns.
If 'revisit' = 0 then order by date - oldest date will be first.
So far I have the 'revisit' alone ordering correctly, and ordering by date if 'revisit' = 0, but ordering by the four columns simultaneously when 'revisit' = 1 does not.
SELECT *
FROM vehicle
ORDER BY
`revisit` DESC,
CASE WHEN `revisit` = 1 THEN `FL` + `FR` + `RR` + `RL` END ASC,
CASE WHEN `revisit` = 0 THEN `date` END ASC
Instead it seems to be ordering by the total of the four columns (which would make sense given addition symbols), so how do I ORDER BY these columns simultaneously, as individual columns, rather than a sum.
I hope this makes sense and thanks!
In your current query, you order by the sum of the four columns. You can use least to get the lowest value, so your order by clause could look like:
SELECT *
FROM vehicle
ORDER BY
`revisit` DESC,
CASE WHEN `revisit` = 1 THEN LEAST(`FL`, `FR`, `RR`, `RL`) END ASC,
CASE WHEN `revisit` = 0 THEN `date` END ASC
Of course this would sort only by the lowest value. If two rows would both share the same lowest value, there is no sorting on the second-lowest value. To do that is quite a bit harder, and I didn't really get from your question whether you need that.

Max value from varchar

i have a column in a database in varchar format, but the content is only numbers.
I cant change the column to int format, and need to use the varchar format.
How it possible to find the highest number?
Category
-------
1
2
3
4
5
6
7
8
9
10
11
12
if I use this
SELECT * FROM [Navision4].[dbo].[" & strCompany & "$eShopCategory]
ORDER BY Category desc
it my output 10, but i want the output to be 12.
What if you CAST category column to INT while performing sorting like
SELECT *
FROM [Navision4].[dbo].[" & strCompany & "$eShopCategory]
ORDER BY CAST(Category AS INT) desc
You can as well use MAX() function on Category column
SELECT MAX(CAST(Category AS INT))
FROM [Navision4].[dbo].[" & strCompany & "$eShopCategory]
select category from mytable
order by length(category) desc, category desc
limit 1
(or len(catgegory) if DB is actually SQL Server)

Returning field name when using Max and Count queries

Cant seem to figure this one out...
I have a table, one of the fields is variabletype. There are several user input variable types. For example:
id | variabletype
1 | button
2 | text
3 | button
4 | link
5 | button
6 | link
I wrote some SQL to basically count the number of times each variable type is listed. I nestled that into a subquery so that I can then get the record that has the max number of instances (in this example - button).
My problem is the query only returns the max number, it does not display the actual variable type. my ideal outcome is having the variabletype display along with the max count. Any thoughts?
SELECT MAX(y.mosttested)
FROM (SELECT variabletype, COUNT(variableid) AS mosttested
FROM variable GROUP BY variabletype) y
Try this :
SELECT `variabletype `,
COUNT(`variabletype `) AS `value_occurrence`
FROM `my_table`
GROUP BY `value`
ORDER BY `value_occurrence` DESC
LIMIT 1;
Try
select variabletype,n
from
(SELECT variabletype, COUNT(variableid) AS mosttested
FROM variable GROUP BY variabletype) v
where mosttested=(SELECT MAX(y.mosttested) AS n
FROM (SELECT variabletype, COUNT(variableid) AS mosttested
FROM variable GROUP BY variabletype) y)
If row_number function is avialable in your SQL environment then you can do the following:
SELECT y.variabletype, y.mosttested as max_mosttested
FROM (SELECT variabletype, COUNT(variableid) AS mosttested, row_number()over(order by count(variableid) desc)
FROM variable GROUP BY variabletype) y
where rownum=1

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10

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.