Jump to content

[SOLVED] Date queries not adding up


lemmin

Recommended Posts

I am trying to query a DB for the number of rows that exist where a date field is between two values.

 

Here is the code:

$query = "SELECT COUNT(*) as ct FROM users WHERE registrationDate >= '2008-07-01' AND registrationDate <= '2008-09-30' AND userType = 3";
$result = mysql_query($query) or die (mysql_error());
echo mysql_result($result, 0) . "<br>";

$query = "SELECT COUNT(*) as ct FROM users WHERE registrationDate >= '2008-10-01' AND registrationDate <= '2008-12-31' AND userType = 3";
$result = mysql_query($query) or die (mysql_error());
echo mysql_result($result, 0) . "<br>";

$query = "SELECT COUNT(*) as ct FROM users WHERE registrationDate >= '2009-01-01' AND registrationDate <= '2009-03-31' AND userType = 3";
$result = mysql_query($query) or die (mysql_error());
echo mysql_result($result, 0) . "<br>";

$query = "SELECT COUNT(*) as ct FROM users WHERE registrationDate >= '2009-04-01' AND registrationDate <= '2009-06-30' AND userType = 3";
$result = mysql_query($query) or die (mysql_error());
echo mysql_result($result, 0) . "<br>";

$query = "SELECT COUNT(*) as ct FROM users WHERE registrationDate >= '2008-07-01' AND registrationDate <= '2009-06-30' AND userType = 3";
$result = mysql_query($query) or die (mysql_error());
echo mysql_result($result, 0);

 

Here is the output:

98

261

303

195

878

 

The first four queries are supposed to be grabbing the numbers for each quarter of the year and the last one grabs the total. The last query returns 878, but when you add up the first four numbers the total is 857.

 

Does anyone have any idea why the numbers aren't the same?

 

Thank you.

Link to comment
Share on other sites

I have a theory.  What is the type of the registrationDate column?  Is it a DATETIME or TIMESTAMP?  If so, there is your answer -- you have registrations that occurred in the last 23 hours and 59 seconds of the last day of the month.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.