Ron van der Heijden
Ron van der Heijden

Reputation: 15080

Get cell value in MySQL query where date

I have the following table:

-----------------------------
| id  | deadline   | Notify |
-----------------------------
| 1   | 2017-06-16 | 4      | // true
| 2   | 2017-06-17 | 5      | // true
| 3   | 2017-06-18 | 5      | // false -> tomorrow
| 4   | 2017-06-19 | 6      | // false -> tomorrow
-----------------------------

The notify column present the days before the deadline when a notification should be sent.

Using the following query gives me the record for all the deadlines in 5 days

SELECT * FROM `issues` WHERE deadline = DATE(NOW() + INTERVAL 5 DAY)

How can I use the cell information of the same row? So that, in this example, I get the rows #1 and #2?

Upvotes: 1

Views: 79

Answers (2)

Mazai E S
Mazai E S

Reputation: 49

The deadline - current date gives the number of days before expiring. So try

SELECT * FROM `issues` WHERE DATE(deadline) - DATE(NOW()) <= 5 

Upvotes: 0

Giorgos Betsos
Giorgos Betsos

Reputation: 72185

Try this:

SELECT *
FROM issues
WHERE deadline - INTERVAL Notify DAY <= NOW() 

The above query returns rows 1, 2. If you want to return all rows with a true, false flag then you can try the following query:

SELECT *,
       IF(deadline - INTERVAL Notify DAY <= NOW(), true, false) AS flag
FROM issues

Demo here

Upvotes: 2

Related Questions