SQL oder by ASC over 0, then 0 - mysql

I need an SQL query that's do an ORDER ASC > 0, then = 0 at the end to the query result.
SELECT * FROM TABLE WHERE rank>0 ORDER BY rank ASC
and then, put at the end the ones whose rank is 0
Name Rank
Martin 0
Bob 2
Marc 8
Mario 0
Sophia 4
After the query :
Name Rank
Bob 2
Marc 4
Sophia 8
Martin 0
Mario 0
Thanks !

Just do this:
ORDER BY rank=0 ASC, rank ASC
The expression rank=0 evaluates as a boolean expression, returns 0 for FALSE, 1 for TRUE, or NULL.
The more ANSI compliant equivalent syntax for rank=0 would be:
CASE WHEN rank = 0 THEN 1 WHEN rank IS NULL THEN NULL ELSE 0 END
Given the example data, this shows how the ordering by rank=0 achieves the result.
Name Rank rank=0
------- ---- ------
Bob 2 0
Sophia 4 0
Marc 8 0
Mario 0 1
Martin 0 1
(NOTE: the order of the rows withing equal values of rank is indeterminate; there's no guarantee of the order of Mario and Martin... either way is compliant with the ORDER BY specification.)

Try following
SELECT *, IF(rank>0,1,0) as Ordering FROM TABLE ORDER BY Ordering DESC, rank ASC
You can then change the ordering with Ordering DESC to bring the results with ZERO rank either at the beginning or end of the result set

Related

Mysql overwrite next rows in select

i have the following table
id
name
flag
1
carl
0
2
mark
1
3
steve
1
what I want to do is the first row would override the succeeding row value for flag
so the result would be something like this
id
name
new_inherited_flag
original_flag
1
carl
0
0
2
mark
0
1
3
steve
0
1
how do i achieve this?
You could just use a subquery to find the earliest flag value:
SELECT id, name,
(SELECT flag FROM yourTable ORDER BY id LIMIT 1) AS new_inherited_flag,
flag AS original_flag
FROM yourTable
ORDER BY id;

MySQL: Order by specific items first then by time

Lets say we have a table
names
-------------------
id name created_at
1 alpha 2020-10-23 17:30:35
2 beta 2020-10-24 17:30:35
3 gamma 2020-10-25 17:30:35
4 kilo 2020-10-26 17:30:35
5 charlie 2020-10-27 17:30:35
6 hector 2020-10-28 17:30:35
I want to order the first few rows by a fixed array let's say 6,3,2 and the rest by created_at in desc order.
So the order that I'm expecting would be 6,3,2,5,4,1.
How can I achieve this using Mysql ?
I tried using field() but can't get it to work with the other column.
FIELD() is tricky for this, because it returns 0 if there are no matches. You can construct an expression that does what you want:
order by coalesce(nullif(field(id, 6, 3, 2), 0), 999999),
created_at desc
If you know that the ids are always descending for the fixed values, then you can use:
order by (case when id in (6, 3, 2) then id end) desc,
created_at desc
SELECT * FROM names ORDER BY (
CASE
WHEN id = 6 THEN 1
WHEN id = 3 THEN 2
WHEN id = 2 THEN 3
ELSE 4
END, created_at DESC
)
The CASE statement ensures the first 3 items listed are 6, 3 and 2, and the rest are listed in created_at DESC order.
One option would be to write a case expression as follows
select *
from names
order by case when id in (6,3,2) then 0
else 1
end asc,created_at desc

How to fetch MAX rank using case statement mysql

Already checked solutions related to my question but didn't get what I want to achieve
Here is my table structure (for ex. where table name is tbl_rank)
name rank
apple 10
banana 2
grapes 5
orange 1
chiku 0
pineapple 0
Now What I want as result is that the elements with 0 rank should be given the max rank and all the elements should appear in descending order of their rank (actually creating a new column as new_rank),
This is the desired result
name rank new_rank
apple 10 10
chiku 0 10
pineapple 0 10
grapes 5 5
banana 2 2
orange 1 1
To get the above result I am using this query
SELECT *, (CASE WHEN rank=0 THEN MAX(rank) ELSE rank END) as new_rank
FROM `tbl_rank` ORDER BY new_rank DESC
but all I am getting is only one row as result of the above query
No idea where I am doing wrong or why its returning only one row
Try this:
SELECT A.*, (CASE WHEN A.rank = 0 THEN B.rank ELSE A.rank END) AS new_rank
FROM tbl_rank A, (SELECT MAX(rank) AS rank FROM tbl_rank) AS B
ORDER BY new_rank DESC;

Can anyone help me to write sql statement?

Can anyone help me to write sql below?
Suppose:
tbl_request
No Title Date Priority
1 AAA 2013-08-06 3
2 BBB 2013-08-04 1
3 CCC 2013-08-05 0
4 DDD 2013-08-02 4
5 EEE 2013-08-01 2
6 FFF 2013-08-04 0
7 GGG 2013-08-03 5
8 HHH 2013-08-03 0
There are two top priorities to order in sql statement:
1st priority: Ordering by Priority in Ascending (only 1,2,3,4,5)
2nd priority: Ordering by Date Descending
I want to show all request that order with the 1st priority first (0 won't display). After 1st priority, i want to display the 2nd priority.
Here is what i want:
No Title Date Priority
2 BBB 2013-08-04 1
5 EEE 2013-08-01 2
1 AAA 2013-08-06 3
4 DDD 2013-08-02 4
7 GGG 2013-08-03 5
3 FFF 2013-08-05 0
6 GGG 2013-08-04 0
8 HHH 2013-08-03 0
I don't know how to write sql statement to get the format above. Can anyone tell me how to write it?
Thank in advance.
How about something like
SELECT *
FROM Table1
ORDER BY
CASE WHEN `Priority` != 0 THEN NULL ELSE 1 END,
`Priority`,
`Date` DESC
or
SELECT *
FROM Table1
ORDER BY
CASE WHEN `Priority` != 0 THEN 0 ELSE 1 END,
`Priority`,
`Date` DESC
SQL Fiddle DEMO
This will ensure that even if any of the priorities are greater than the other answers max values, this will still sort 0 as last.
You could try something like:-
SELECT * FROM tbl_request
WHERE Priority IN (1, 2, 3, 4, 5)
ORDER BY Priority, DATE DESC
UNION
SELECT * FROM tbl_request
WHERE Priority NOT IN (1, 2, 3, 4, 5)
ORDER BY Date DESC;
select no,title,date,priority
from tbl_request
order by if(priority=0,99999,priority),date desc
This assumes that no priority is over 99999. Yo also need to pit back quotes around no and date columns - I don't have them on this keyboard!
You should use case expression as below
select No,Title,Date,Priority
from tab
order by
case
when Priority=0 then 9999
else Priority
end,
Date desc
If you aren't displaying 0 Priority records, then the below SQL statement will do just fine.
SELECT * FROM tbl_request WHERE Priority <> 0 ORDER BY Priority ASC, Date DESC
You can try:
Select title, date, priority from tbl_request Order By IF(priority = 0, 99999, priority) asc, date desc
though it is ugly, inefficient, it works for you. Or you can consider making a union of to sub queries
Select title, date, priority from tbl_request where priority > 0 order by priority asc
union
Select title, date, priority from tbl_request where priority = 0 order by date desc
SELECT *
FROM Table1
ORDER BY
(CASE WHEN Priority = 0 THEN 9999 ELSE 1 END) asc, date desc
SELECT *
FROM Table1
ORDER BY field(`Priority`,0),`Priority`,
`Date` DESC
FIDDLE
Select * from tbl_request
Order by ( CASE WHEN Priority > 0 THEN Priority ELSE 99999999 END ), DATE DESC

mysql random with condition

I have a table in mysql, say table1.
I am running this on it:
SELECT FLOOR( MAX(id) * RAND()) FROM `table1`
This works well, but I am now trying to add a condition of "AND tom".
Where tom is a integer field.
For example:
id tom
1 0
2 3
3 2
4 0
5 0
6 3
7 1
8 1
9 3
etc.
So, my question is,
How can I pick a random value from id, which also satisfies tom='0' say?
SELECT id FROM `table1` WHERE tom = 0 ORDER BY RAND() LIMIT 1
This will first get all rows in which tom = 0,then order those results randomly. MySQL will then limit those results to just one, returning the single value you want to retrieve.
I hope I understood correctly:
SELECT id FROM `table1` WHERE tom = 0 order by rand() limit 1
select * from (
select * from table where tom = 0 ) as t order by rand() limit 1