Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieve rows based on month/year from mysql

Posted on 2012-04-10
4
Medium Priority
?
578 Views
Last Modified: 2012-04-11
Hello,

I'm trying to construct a query to retrieve all the call costs incurred during a particular month passed from a post (post will look like: mydomain.com/script.php&timeperiod=2012-01 for instance) .

The timestamp for each call that has come in during the month is stored in the DB like: 2012-01-17 10:36:21

Not sure how to construct the 'WHERE' part to just look at the month/year and not the day or time?

Here is what I have currently.

$timeperiod = $_REQUEST['timeperiod];

$result = mysql_query("SELECT SUM(CallCost) FROM callers WHERE account_id = '$account_id' AND CallDate = '$timeperiod'");
            while ($row = mysql_fetch_assoc($result)) {
            $totalcost = $row['SUM(CallCost)'];
            $totalcost = sprintf("%01.2f", $totalcost);
      }

So with the above, I'd be trying to retrieve all the call costs that occurred in the month of January 2012 only and totalling them up.

Thanks as always,
TJ
0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:yawkey13
ID: 37830574
Your query will be:
SELECT SUM(CallCost) FROM callers WHERE account_id = '$account_id' AND MONTH(CallDate) = 'right($timeperiod,2)' AND YEAR(CallDate) = 'left($timeperiod,4)'

Here are the right and left functions:
function right($value, $count){
    return substr($value, ($count*-1));
}
 
function left($string, $count){
    return substr($string, 0, $count);
}

The query is solid, but check my PHP.  It is not my strong point.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37830675
Please read this article:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

You can get the first day of a month with something like this:
URL: ...&timeperiod=2012-01
$alphaperiod = date('c', strtotime($_GET['timeperiod']  . '-01'));

And the last day of a month with something like this:
$omegaperiod = date('c', strtotime($_GET['timeperiod'] . '-t'));

Then you can use these values in the WHERE clause like this:
WHERE CallDate BETWEEN '$alphaperiod' AND '$omegaperiod'
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 37830741
Wish I read that couple months ago :)
Thanks.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37831868
Thanks for the points!  The way PHP functions date() and strtotime() play together is almost magical.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question