Solved

How to sort data from Mysql table by month

Posted on 2004-03-26
6
528 Views
Last Modified: 2013-12-12
Hello there,

I have created a PHP page that retrieves two columns from a MySQL db (date and amount). What i can't seem to comprehend is the next step... how to sum the amount and show it by month name and year as below.
The Mysql table i have consists of three columns:

-ID Autoincrement
-Date Date
-Minutes Int(11)

This is how i would like to have the data.

January 2001              45673
February 2001            34455
March 2001                23747
April 2001                  86464
May 2001                   6363
June 2001                   0
etc..

Here is my PHP code so far...
----------------------------------
<?php
// connect to the database
include("../include/irm.inc");

$query = "SELECT Date, Minutes FROM tbl_Minutes ORDER BY Date";

$sth = $adb->prepare($query);
if($sth)
{
        $res = $sth->execute();
        $numrows = $sth->rows();
        for($i = 0; $i < $numrows; $i++)
          {
                $result = $sth->fetchrow_hash();
                $Date = $result["Date"];
                $Minutes = $result["Minutes"];
                PRINT "$Date  ";
                PRINT "$Minutes <BR>";
         }
        $sth->finish();
} else
{
    PRINT "Could not prepare query: ".$sth->errstr."<br>\n";
}
?>
------------------------------------------
Any pointers, ideas or examples would be greatly appreciated. Thanks alot.
Richard.
0
Comment
Question by:richardwalton
[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
  • 2
6 Comments
 
LVL 6

Accepted Solution

by:
CosminB earned 150 total points
ID: 10688288
try this query:
$query = "SELECT date_formt(Date, '%M %Y') as Date , Minutes FROM tbl_Minutes ORDER BY Date";

read this for more info: http://www.mysql.com/doc/en/Date_and_time_functions.html
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 150 total points
ID: 10689980
Just a little change of CosminB's answer to add up the minutes for you:

SELECT date_formt(Date, '%M %Y') as Date , sum(Minutes) as Minutes FROM tbl_Minutes GROUP BY Date
0
 
LVL 27

Assisted Solution

by:skullnobrains
skullnobrains earned 100 total points
ID: 10703559
snoyes, this does not add minutes but rather outputs as a different field the sum of all the minutes values for a given date...

richard, can't you store timestamps in the db and convert them afterward ? it usually ends up to be the best solution to avoid problems (of course the type of the field will NOT be 'timestamp' but rather INT(10), atherwise the timestamps will automatically change upon update )

otherwise, cosmin's solution will work as long as mysql properly interprets the content of the field (beware you can't mispell January for example)

good luck
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10705837
skullnobrains:

Quoting the question (emphasis mine):
>>>What i can't seem to comprehend is the next step... how to SUM THE AMOUNT and show it by month name and year as below.
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 10706811
most sorry snoyes, u were pertinent as usual and i misunderstood the question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to use jquery variable in mysql query 20 77
php output utf-8 problem 6 51
PHP URL Parameter to simple URL for single folder 16 48
Can't Find the Homepage on my site 4 46
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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