Jump to content

get last month


jeff5656

Recommended Posts

I have a date field.  How do I select all records from last month (in this example it would be all records from April).

 

Something like $query = "select * from table where date_field = last month"

 

Thank you!

 

And moderators: aren't you proud of me for posting this here instead of php coding help? :-)

Link to comment
Share on other sites

I tried this but got an error:

 

$query = "select * from dos  WHERE month(billing_level) = '04'";

 

the field is billing_level.

I got this error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

Link to comment
Share on other sites

The query you tried will match April of 2011, April of 2010, April of 2009,...

 

Matching dates generally requires testing the year as well -

 

SELECT * FROM dos WHERE EXTRACT(YEAR_MONTH FROM your_date_field) = EXTRACT(YEAR_MONTH FROM DATE_SUB(CURDATE(), INTERVAL 1 MONTH))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.