+ Reply to Thread
Results 1 to 5 of 5

Thread: Getting '0' results from MySQL COUNT()

  1. #1
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Question Getting '0' results from MySQL COUNT()

    I have a table with a timestamp column. I would like to get the count of the number of rows in the table corresponding to each of the past 7 days. My problem is that I get no row if there are none, instead of {[date], 0}. Can anyone provide me with a clever solution to this?

    Current MYSQL query:
    Code:
    SELECT DATE(submit_dt) AS date, COUNT(id)
    FROM db_itemtable
    WHERE DATE(submit_dt) > DATE(TIMESTAMPADD(DAY,-4,NOW()))
    GROUP BY date;
    Example current return:
    2009-10-25, 2
    2009-10-24, 11
    2009-10-22, 6

    What I want returned:
    2009-10-25, 2
    2009-10-24, 11
    2009-10-23, 0
    2009-10-22, 6

    After I have the existing query, I can use PHP to add in the empty rows, but that seems a bit wasteful and inelegant.

  2. #2
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Re: Getting '0' results from MySQL COUNT()

    i would just stick to the solution that you are using for now, because if what im thinking is right it would take a lot more coding, and it would just be easier to use what your using now.

  3. #3
    suomiaol is offline x10Hosting Member suomiaol is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    3

    Smile Re: Getting '0' results from MySQL COUNT()

    I was working on this for a minute, and then I finally figured it out... (heh, yes I'm sleepy that is why this took a while):

    The problem is with your where clause, if you have zero records per day your SQL will not return any rows. If you have a timestamp for a day it will automatically return count >=1 (right?)

    In general for casting a null value you can use e.g. COALESCE - function, ISNULL or IFNULL.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Getting '0' results from MySQL COUNT()

    Quote Originally Posted by suomiaol View Post
    The problem is with your where clause, if you have zero records per day your SQL will not return any rows. If you have a timestamp for a day it will automatically return count >=1 (right?)

    In general for casting a null value you can use e.g. COALESCE - function, ISNULL or IFNULL.
    The problem isn't a NULL value, it's (as you note in the first sentence above) lack of rows. COALESCE, IFNULL & al. won't help. Not much will help. The OP would need to generate rows for the dates, and SQL is more for manipulating data than generating it. For this reason, an SQL solution will most likely be unwieldy or inelegant. With a table of dates from over the week, it could be simple, but that begs the question.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Getting '0' results from MySQL COUNT()

    Code:
     
    SELECT COUNT( * ) AS count, 
           ADDDATE( CURDATE(), 0)  AS date
    FROM `testing` 
    WHERE DATE( submit_dt ) = ADDDATE( CURDATE(), 0)
     
    
    UNION
     
    SELECT COUNT( * ) AS count, 
           ADDDATE( CURDATE(), -1)  AS date
    FROM `testing` 
    WHERE DATE( submit_dt ) = ADDDATE( CURDATE(), -1)
     
    UNION
     
    SELECT COUNT( * ) AS count, 
           ADDDATE( CURDATE(), -2)  AS date
    FROM `testing` 
    WHERE DATE( submit_dt ) = ADDDATE( CURDATE(), -2)
     
    UNION
     
    SELECT COUNT( * ) AS count, 
           ADDDATE( CURDATE(), -3)  AS date
    FROM `testing` 
    WHERE DATE( submit_dt ) = ADDDATE( CURDATE(), -3)
    Assuming you are starting with today and want four days total.
    Can be generated by a PHP loop.
    Last edited by descalzo; 10-28-2009 at 11:16 AM.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. how to ignore empty results from MySQL query in PHP?
    By stevet70 in forum Programming Help
    Replies: 3
    Last Post: 05-17-2009, 04:11 PM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers