'SQL query get rows after and before number of days
I'm inheriting an SQL query that gets data from my database table based on the number of days before the current date, I'd like to extend this functionality to also include data after the current date, I've tried extending it by adding a new line but my SQL query doesn't return any data, here's the current query:
SELECT
IF((
SELECT
send.id AS "true"
FROM
sends send
WHERE
send.sent = 0 AND
send.subscription_id = subscription.id
LIMIT 1
), true, false) AS already_queued
FROM
subscriptions subscription
WHERE
subscription.unsubscribed = 0 AND
subscription.source_id = 12 AND
DATE(subscription.date_added) <= (CURRENT_DATE - INTERVAL 1 DAY)
ORDER BY
subscription.date_added
DESC
Right now it's the following line that handles the data after a number of days:
DATE(subscription.date_added) <= (CURRENT_DATE - INTERVAL 1 DAY)
Where 1 is replaced dynamically with the value, I now want to add a new line:
DATE(subscription.date_added) >= (CURRENT_DATE - INTERVAL 7 DAY)
So this should return data between the two dates but doesn't, what am I missing from the combined query to make it work:
SELECT
IF((
SELECT
send.id AS "true"
FROM
sends send
WHERE
send.sent = 0 AND
send.subscription_id = subscription.id
LIMIT 1
), true, false) AS already_queued
FROM
subscriptions subscription
WHERE
subscription.unsubscribed = 0 AND
subscription.source_id = 12 AND
DATE(subscription.date_added) >= (CURRENT_DATE - INTERVAL 7 DAY) AND
DATE(subscription.date_added) <= (CURRENT_DATE - INTERVAL 1 DAY)
ORDER BY
subscription.date_added
DESC
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
