Display only current month and next month using MySQL query

Hi Guys,

I have this query:

SELECT contacts.id, salutation, first_name, last_name, contacts_cstm.id_c, customerstatus_c, DATE_FORMAT(STR_TO_DATE(expirydate_c, "%d/%m/%Y"), "%M %d, %Y") as expiration_date
FROM contacts, contacts_cstm
WHERE contacts.id = contacts_cstm.id_c
AND contacts_cstm.customerstatus_c = "Paying Customer"
ORDER BY contacts.first_name

The column expirydate_c is a varchar type field, so that’s i am using the date_format and str_to_date format. Right now, it will just display all Paying customer no matter what date it is.

I just want limit the display to this month and next month. How will I do that in MySQL query?

Thanks

Try this:

SELECT contacts.id, salutation, first_name, last_name, contacts_cstm.id_c, customerstatus_c, DATE_FORMAT(STR_TO_DATE(expirydate_c, "%d/%m/%Y"), "%M %d, %Y") as expiration_date
FROM contacts, contacts_cstm
WHERE contacts.id = contacts_cstm.id_c
AND contacts_cstm.customerstatus_c = "Paying Customer"
AND STR_TO_DATE(expirydate_c, "%d/%m/%Y") >= [start date] 
AND STR_TO_DATE(expirydate_c, "%d/%m/%Y") <= [end date]
ORDER BY contacts.first_name

Obviously, replace [start date] & [end date] with the relevant dates.

You’re doing a lot of conversion here switching back and forth from DATE to STRING. It might be easier to just select using the string itself then convert it in PHP.

Try this:

SELECT contacts.id, salutation, first_name, last_name, contacts_cstm.id_c, customerstatus_c, expirydate_c as expiration_date
FROM contacts, contacts_cstm
WHERE contacts.id = contacts_cstm.id_c
AND contacts_cstm.customerstatus_c = "Paying Customer"
AND expiration_date LIKE '%NAME_OF_CURRENT_MONTH%'
ORDER BY contacts.first_name