Nirjhor
Nirjhor

Reputation: 73

How to get Number of Rows after GROUPING (using GROUP BY) a SQL Query?

CODE:

$result = mysql_query("SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp
   FROM `cdr_data_january` 
   WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
   GROUP BY `user_id` 
   UNION 
   SELECT COUNT(`user_id`), `mac_add`, SUM(`duration`)/60 AS ConnTime, SUM(`download`)/1000000 AS TotalDown, SUM(`upload`)/1000000 AS TotalUp 
   FROM `cdr_data_february` 
   WHERE (`bs_id_site` LIKE '".$btsid."') AND (`ldate` BETWEEN '".$date1."' AND '".$date2."') 
   GROUP BY `user_id`");

I have such a MySQL Query that I need to run on PHP.

I need to get the Number of Row after GROUPING (i.e. after using GROUP BY).

$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

This is returning me the Total Number of Rows (before GROUPING) in fact.

I am not understanding that what should be the code to receive the Row Number (after GROUPING).

Note for a Case: For a query it is returning 15 as $numrows, which is the total number of rows before GROUP BY, but I need that $numrows will return 5, which is the number of rows after GROUP BY.

If someone knows the solution please help me to solve it. Thanks.

Upvotes: 0

Views: 389

Answers (1)

designosis
designosis

Reputation: 5263

You can use ...

$num_rows = mysql_num_rows($result);

... which returns total row count.

Upvotes: 2

Related Questions