Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select all dates that are an increment of x days

Tags:

date

sql

mysql

Is it possible to query for all dates in the future that are an increment of x days?

i.e.

SELECT * 
FROM bookings 
WHERE date >= CURDATE()
AND 
(
       date = CURDATE() + INTERVAL 6 DAY 
   OR  date = CURDATE() + INTERVAL 12 DAY
   OR  date = CURDATE() + INTERVAL 18 DAY
   etc.
)
like image 740
Rwd Avatar asked May 26 '14 19:05

Rwd


People also ask

How do I increment in MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

How do I get Currentdate in MySQL?

Simply use the CURDATE() function to get the current date. The date can be displayed in two different formats: ' YYYY-MM-DD ' if it is used in a string context or YYYYMMDD if it is used in a numeric context. There are two other functions that can be used instead of CURDATE() : CURRENT_DATE and CURRENT_DATE() .

What is X in MySQL?

The optional MySQL Server X Plugin is the latest one. It implements its own client server protocol called the X Protocol. Application clients and their drivers feature the alternative protocol and a new X DevAPI programming API. The X DevAPI is an alternative way of acessing MySQL.


1 Answers

Something like:

SELECT
     *
FROM table
WHERE
    date >= CURDATE()
    AND
    DATEDIFF(CURDATE(), date) % 6 = 0

Datediff returns the number of days difference, and % 6 says return the remainder when divided by six.

like image 61
scragar Avatar answered Oct 06 '22 17:10

scragar