Ordering results differently in MySQL - mysql

I have a table which has 3 columns. (day, month and year)
This statement gives results in chronological order.
SELECT * FROM table WHERE = 'condition' ORDER BY year, month, day
How can I get it in the inverse order?

You need to invert your sort order in your query:
SELECT * FROM table WHERE = 'condition' ORDER BY year DESC, month DESC, day DESC
Having separate columns for year, month, and day is counter-productive, though, as all of these could be represented in a singular DATE type column. This can be indexed and is much faster in practice:
SELECT * FROM table WHERE ... ORDER BY date_column DESC

Try this:
SELECT * FROM table
WHERE = 'condition'
ORDER BY year DESC, month DESC, day DESC

You can use the ASC or DESC keywords.
https://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html
For example:
SELECT *
FROM table WHERE = 'condition'
ORDER BY year DESC,
month DESC,
day DESC

Related

How to select five values of a column with the top 5 in their corresponding row?

So I have two columns date and norder in my SQL table orders. We have to select dates with the top 10 maximum orders. We can do this like
SELECT date
FROM orders
ORDER BY norder DESC
LIMIT 10;
But we want to order these 10 dates such that the date is in descending order on values that have the same norder. How should we do this?
This should do.
SELECT date
FROM orders
ORDER BY order DESC, date DESC
LIMIT 10;
You can use window functions.
select a.date from
(select date,dense_rank() over (order by norder desc) as rank
from orders
order by rank desc) as a
where rank <= 10
order by date desc;
This would give you the dates for the top 10 maximum orders (including dates for orders where the maximums are identical).
In Microsoft SQL Server:
SELECT TOP(10) [date]
FROM [orders]
ORDER BY [order] DESC, [date] DESC;

SQL retrieve all rows between two dates and a specific number of rows before the date ordered by date

As the title says, I have a table with a date column. I am trying to retrieve all rows between 2 dates as well as X number of rows before the beginning date ordered by date.
Take a select * from table order by date DSC
20200201
20200101
20191201
20191101
20191001
20190901
20190801
20190701
I want between 20200201 and 20191201 and the previous 3 rows(not knowing the date)
result
20200201
20200101
20191201
20191101
20191001
20190901
my current query returns a random set of high dates between the union for some reason:
(SELECT * FROM Table WHERE Date BETWEEN 20200201 AND 20191201 ORDER BY Date ASC)
UNION
(SELECT * FROM Table WHERE Date < 20191201 ORDER BY Date DESC LIMIT 3)
Any idea where I am going wrong?
One method uses lead():
select t.*
from (select t.*,
lead(date, 3) over (order by date) as next_date_3
from t
) t
where (date < '202o-02-01' and (next_date_3 >= '2020-02-01' or next_date_3 is null) ) or
(date between '2020-02-01' and '2019-12-01')
I tried this query and it worked for me:
(SELECT * FROM Table WHERE Date BETWEEN 20191201 AND 20200201 ORDER BY Date ASC)
UNION
(SELECT * FROM Table WHERE Date < 20191201 ORDER BY Date DESC LIMIT 3)
The first select shows me:
20191201
20200101
20200201
The second select shows me:
20191101
20191001
20190901
The issue was as jarlh pointed out, the order of the subquery's were not kept(thats why things were jumbled) and a simple ORDER BY of the whole UNION fixed this. I did not try Gordon's response as this solved my issue so:
(SELECT * FROM Table WHERE Date BETWEEN 20191201 AND 20200201)
UNION
(SELECT * FROM Table WHERE Date < 20191201 ORDER BY Date DESC LIMIT 3)
ORDER BY Date ASC
Thank you

Order by multiple columns with multiple conditions

I have a table with some events like this
id----------title-----------date-------------status
1-----------birthday-------2018-03-12--------1
2-----------match----------2018-03-13--------2
3-----------anniversary----2018-03-10--------1
4-----------trip-----------2018-03-15--------1
5-----------birthday-------2018-03-17--------2
6-----------birthday-------2018-03-11--------1
Expected Result
id----------title-----------date-------------status
1-----------birthday-------2018-03-12--------1
4-----------trip-----------2018-03-15--------1
5-----------birthday-------2018-03-17--------2
2-----------match----------2018-03-13--------2
6-----------birthday-------2018-03-11--------1
3-----------anniversary----2018-03-10--------1
I need to query it like the first rows which have dates greater than today with status 1 should appear first and then the rest in desc.
Suppose today is 2018-03-11 then row with id 1 should appear first and then the rest of the rows is desc order
This is what I have tried so far
SELECT *
FROM events
ORDER BY (date > CURDATE() and status = 1) asc,
date desc
You can use multiple keys in an order by:
order by (date >= curdate() and status = 1) desc,
date desc
I believe your SQL should be something like this but is hard to say without expected results.
Query
SELECT
*
FROM
[table]
WHERE
date > CURDATE()
AND
status = 1
ORDER BY
date ASC
LIMIT 1
UNION
SELECT
*
FROM
[table]
WHERE
id NOT IN (
SELECT
id
FROM
[table]
WHERE
date > CURDATE()
AND
status = 1
LIMIT 1
)
AND
date > CURDATE()
ORDER BY
date DESC

mySQL UNION clause with two ORDER BY clause

here's the problem
I need to fetch rows from my table where date created is less than 24 hours ago and ORDER them by likes and then UNION them with the rest of rows in the same table but I want the remaining rows to be ORDERED by date created.
in other words I need separate ORDER BY clause for each SELECT.
to be more clear, it's something like this:
SELECT *
FROM (SELECT *
FROM 'table'
WHERE 'date_created' > timestampadd(hour, -24, now())
ORDER BY 'likes' DESC
UNION
SELECT *
FROM 'table'
WHERE
ORDER BY date_created DESC ) AS Results
LIMIT 10 OFFSET 0
thanks for this awesome community :)
Try this:
SELECT *, (date_created > timestampadd(hour, -24, now()) AS recent
FROM table
ORDER BY recent DESC, IF(recent, likes, 0) DESC, IF(!recent, date_created, 0) DESC
LIMIT 10 OFFSET 0

MySQL Order by column = x, column asc?

I won't paste the whole query. It looks like this.
SELECT *, MONTH(date_created) as month
from table
GROUP BY month ORDER BY month='3', month asc
As it is April and I am querying this year I would have expected the order to be as follows 3, 1, 2, 4.
Instead I get out 1, 2, 4, 3.
How can I change the ORDER BY part of the statement to order the results by selected month first then the rest of months in the year showing sequentially?
add DESC
ORDER BY month = '3' DESC, month asc
month='3' is a boolean expression which returns 1 or 0, so basically when the result is zero, it will on the last part of the result.
or without using DESC, use <>
ORDER BY month <> '3', month asc
You have to add DESC or ASC in the first order :
SELECT *, MONTH(date_created) as month
FROM table
GROUP BY month ORDER BY month='3' DESC, month ASC
The solution is ORDER BY FIELD
SELECT * FROM table
GROUP BY month
ORDER BY FIELD(month, 3,1,2,4)