0

So I have been given just the expiry dates for my loyalty cards, I need the query to display all the customers whose card will expire 3 months from the current systems date not hardcoded.

Lets say todays 2021-09-30, and the card expiry date for customer1 is 2021-12-12 and the customer2 is 2021-10-18. SO the query will display the name of the customer1 because its card is expiring in the 3rd month.

I have tried a few different methods but I can't seem to find a solution

select DISTINCT c.customername from customers c WHERE c.CardExpDate now() and
datediff(now(),c.CardExpDate) <= -2 order by c.customerID;

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
S6277
  • 3
  • 1
  • Possible duplicate: https://stackoverflow.com/questions/288984/the-difference-in-months-between-dates-in-mysql – guyaloni Sep 30 '21 at 12:08
  • Does this answer your question? [How to get difference between two dates in months using MySQL query?](https://stackoverflow.com/questions/288984/how-to-get-difference-between-two-dates-in-months-using-mysql-query) – Daniel Widdis Jul 11 '22 at 01:58

1 Answers1

0

you can try this

select c.*, timestampdiff(MONTH, now(3), CardExpDate) from customers c where
timestampdiff(MONTH, now(3), CardExpDate) = 3