close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

SQL for last month

Thread began 1/05/2011 7:18 am by Daryl | Last modified 3/30/2011 5:18 am by Daryl | 2638 views | 9 replies |

DarylBeta Tester

SQL for last month

I am using the following query to find records for last month

SELECT * FROM application JOIN company JOIN users WHERE YEAR(application.application_date) = YEAR(CURDATE()) AND MONTH(application.application_date) = MONTH(CURDATE())-1 AND application.consultant_company = company.co_id AND application.consultant= users.id ORDER BY application.application_date DESC



However this does not work this month for last month. Any idea why and what I should change the sql too to get it to work?

Sign in to reply to this post

Ray BorduinWebAssist

That is because you are using:

MONTH(CURDATE())-1

and this being month 1, then that would look for a return value of month 0, when you actually want month 12.

Instead try:

MONTH(CURDATE() - INTERVAL 1 MONTH)

Sign in to reply to this post
Did this help? Tips are appreciated...

DarylBeta Tester

Thanks Ray

But this doesn't show results from December 2010 either.

Sign in to reply to this post

Ray BorduinWebAssist

That is because you would also have to use:

YEAR(application.application_date) = YEAR(CURDATE() - INTERVAL 1 MONTH)

Sign in to reply to this post
Did this help? Tips are appreciated...

DarylBeta Tester

Thanks Ray!!

Would

YEAR(CURDATE() - INTERVAL 2 MONTH)

show data from 2 months ago?

Sign in to reply to this post

Ray BorduinWebAssist

yes it would.

Sign in to reply to this post
Did this help? Tips are appreciated...

DarylBeta Tester

Excellent!

Thanks again!

Sign in to reply to this post

DarylBeta Tester

What is the best code for PHP to do the same?

I am using;

For this month

php:
<?php $date date('F'); echo "$date";?>



For 1 month ago

php:
<?php
$mnth 
date('F');
$prev_mnth date('F',strtotime("-1 months")); // "previous month" works also
echo $prev_mnth;
?>



But they are both showing March today

For 2 months ago

php:
<?php
$mnth 
date('F');
$prev_mnth date('F',strtotime("-2 months")); // "previous month" works also
echo $prev_mnth;
?>



But this shows January as expected...

Sign in to reply to this post

DarylBeta Tester

This solution shows March instead of February too...

php:
<?php
$last_month_time 
mktime(000date("m")-1date("d"),   date("Y"));
$last_month date('F'$last_month_time);
echo 
"$last_month";
?>
Sign in to reply to this post

DarylBeta Tester

This one works!

php:
<?php echo date('F'mktime(000date('m')-11date('Y')));
?>
Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...