www.webdeveloper.com
Results 1 to 4 of 4

Thread: Mysql get records or data by monthly

  1. #1
    Join Date
    May 2010
    Posts
    23

    Angry Mysql get records or data by monthly

    Hi there,

    Is there a way to write a MySQL query to get records by monthly for example:

    Starting from the current month:

    ----- August -----
    Record 1
    Record 2

    ----- July-----
    Record 1
    Record 2


    ----- June -----
    Record 1
    Record 2


    I have a time stamp column in my db table.

    I can get all the records for the current month, using the month function, for a example:

    Code:
    SELECT name, id, dob FROM users WHERE MONTH(regd) = MONTH(CURDATE())


    thanks...

  2. #2
    Join Date
    Apr 2003
    Location
    Rosemount, MN
    Posts
    2,287
    Like this:

    Code:
    select name, id, dob from users order by month(regdate);

  3. #3
    Join Date
    May 2010
    Posts
    23

    Angry

    thanks 'The Little Guy' ,

    one more thing i forgot to mention is i wanted to output this in this format.

    Do i create separate query for each month and then store each month in a array and parse each array to display them.

    (note im using PHP, array will be outputted using a foreach loop )



    thanks again

  4. #4
    Join Date
    Apr 2003
    Location
    Rosemount, MN
    Posts
    2,287
    You might be able to do something like this:

    (untested)
    PHP Code:
    <?php
    $sql 
    mysql_query("select name, id, dob, month(regdate) as cmonth from users order by month(regdate)");
    $array = array();
    $month 0;
    while(
    $row mysql_fetch_assoc($sql)){
        if(
    $row['cmonth'] != $month){
            
    $month $row['cmonth'];
            
    $time mktime(0,0,0,$month);
            echo 
    "--- ".date("F"$time)." ---<br />";
        }
        echo 
    "{$row['id']}{$row['name']}{$row['dob']}<br />";
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles