Is it possible to get the next date from an output? - mysql

I have a sql-query where I ask for some dates.
"SELECT DISTINCT date FROM table WHERE condition ORDER BY date");
The output is:
2007-04-08
2008-04-12
2008-09-27
2009-12-06
2010-01-31
2011-02-27
2011-04-15
Now I'm wondering whether it is possible to get the next date from the output above.
Examples:
1. Today is 2008-12-12. The next date would be 2009-12-06.
2. Today is today/current_date (2011-02-22). The next date would be 2011-02-27.
Have you got an idea how to specify the query?
Thank you.

SELECT DISTINCT date FROM table WHERE dateColumn > CURDATE()
Order by dateColumn desc
If you want only the NEXT date, then you can use LIMIT
SELECT DISTINCT date FROM table WHERE dateColumn > CURDATE()
Order by dateColumn desc
LIMIT 1

Related

MYSQL sort date by future dates first in ASC and old dates then in DESC

I want to sort dates in MYSQL in a way such that, future dates will be sorted first by in ASC order and, then then old dates last in DESC order. Here is the query I used to do, but both date types (future and old) are sorted out in ASC order. How can I fix this?
SELECT id, end_date FROM employee
ORDER BY CASE WHEN DATE(date) > DATE(NOW())
THEN 0
ELSE 1 END, date ASC
First, sort by the boolean expression date <= CURRENT_DATE so that all future dates are on the top and then sort by the absolute difference to the current date:
SELECT *
FROM employee
ORDER BY date <= CURRENT_DATE,
ABS(DATEDIFF(date, CURRENT_DATE));
See the demo.

mysql query - In a table, if a date exist return record else return previous date's record

If user input date is 2021-08-08 (date present in table), it should return records having this date. If user input date is 2021-08-07 (date not present in table), it should return records 2021-08-05 for the date (previous available date).
Sample data:
select *
from `MyTable`
where targetdate = (
select targetdate
from `MyTable`
where targetdate <= #UserInput
order by targetdate desc
limit 1
)
SELECT *
FROM table
WHERE date_column <= #date_input
ORDER BY date_column DESC LIMIT 1
If more than one row matches the date then indefinite one of them will be returned. If you need some definite then you must describe this precisely.

Date using BETWEEN in MYSQL

I want to display the records using two different date. I tried using the Between
select * from billing where select_client = '2' and order_date BETWEEN '01/06/2018' and '30/06/2018' order by id ASC
It returns JULY month records also. I tried used >= and <=. That query also returns same record.
select * from billing where select_client = '2' and order_date >= '01/06/2018' and order_date <= '30/06/2018' order by id ASC
Kindly help me out to get only the records between the two dates. Thanks in advance
You have to convert the strings to a date to compare it:
select * from billing where select_client = '2' and STR_TO_DATE(order_date, '%d/%m/%Y') BETWEEN STR_TO_DATE('01/06/2018','%d/%m/%Y') and STR_TO_DATE('30/06/2018','%d/%m/%Y') order by id ASC
wrong date format. if order_date is mysql DATE then format should be 2018-06-01

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 query latest before date or (if empty then) first after

I'm trying to retrieve an entry with the latest date before a given date, and if it doesn't exist then take the soonest after that given date.
I tried googling it, but I couldn't find this scenario
You can do this with order by and limit:
select t.*
from table t
order by (datecol < #date) desc,
(case when datecol < #date then datecol end) desc,
datecol asc
limit 1;
for latest before given date -
select * from table where date_column<given_date order by date_column desc limit 1
now check the result if no row return then execute following query for soonest after given date -
select * from table where date_column>given_date order by date_column limit 1