1

I have a SQL Query, although it is executing, but how should i verify if it is giving me the desired result. For example: My query

$query3 = "SELECT COUNT(DISTINCT(uid)) AS `num` 
           FROM `user_info` 
           WHERE date(starttime)='2011-10-10'";

In the above query i am trying to count the number of distinct user ID's(uid) from the table named user-infowhere date is 2011-10-10.

How should i display the count which is calculated for the given date?.I need to know it and perform some further operations on it!

Vijay Verma
  • 3,660
  • 2
  • 19
  • 27
user2565192
  • 694
  • 1
  • 8
  • 19

3 Answers3

1
$query3 = "SELECT COUNT(DISTINCT uid) AS `num` 
           FROM `user_info` 
           WHERE date(starttime)='2011-10-10'";


$result = mysql_query($query3);
$row = mysql_fetch_array($result);
echo $row['num'];
Vijay Verma
  • 3,660
  • 2
  • 19
  • 27
0

Just do:

SELECT COUNT(DISTINCT uid) AS `num` 
FROM `user_info` 
WHERE date(starttime)='2011-10-10' 

This SO post goes into some details about how count and count(distinct ...) work. With just count, there is a hidden/assumed function of count(all ... ) actually happening (it's just the default value). If you want to only count distinct things, switch it to the non-default and do the count(distinct ...) instead. I didn't know it existed for my first 6 months of writing sql or so...

Community
  • 1
  • 1
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
0

You can set a variable sing the result...

SET @userVar=SELECT 
                COUNT(DISTINCT uid) 
              FROM 
                user_info 
              WHERE 
                DATE(starttime)='2011-10-10';

Then you can use that variable in your next operation or query.

Ryan
  • 106
  • 4
  • looks like @gloomy.penguin got the same answer, his answer is useful if you want to get the result back, my answer allows you to then use that result in another query. – Ryan Oct 26 '13 at 07:15