Group By Clause in PHP – Info PHP

Introduction

 

I have described in my previous article how aggregate functions work. Basically, aggregate functions provide all the data in a single result set. The aggregate functions data together. To do that you can use two new clauses of the select statement, “group by” and “having” clauses.

 

Group by clause

 

The “group” clause is used to group the rows of a result set based on one or more column expressions. Group by clause determines how the selected rows are grouped. The group by clause follows the where clause. If you want, more than one column or expression will be included in the “group by” clause, separated by commas.

 

Syntax

  1. SELECT columns Name……. from table Name   
  2. Where condition  
  3. Group by group_by_list  
  4. Having condition  
  5. Order by order_by_list  

Example

 

 

Having clause

 

The “Having” clause determines which groups are included in the final result. The having clause comes after the “group by” clause. It will be called when the data is to be grouped.

 

I explain my point using an example. Suppose you have a table with the columns id, name, and salary. You can see that in the first image of this article, in which the emp_dtl table data is shown. The id 102 is duplicated.

 

Then when you apply: “select id, Firstname,avg(salary) as avgsal from emp_dtl group by id;”

 

The following will be the result:

 

group-by-with-having1-clause-in-mysql.jpg

 

Then, if you want the average salary of each group greater then 2600 then use the following query:

 

“select id, Firstname,avg(salary) as avgsal from emp_dtl group by id having(salary)>2600”;

 

group-by-with-having2-clause-in-mysql.jpg

 

So, we say that a “Having” clause will be called when the data is grouped.

 

Example of “Group by” clause in PHP

  1.   
  2. $con=mysql_connect(“localhost”,“root”,“”);  
  3. if (!$con)  
  4.   {  
  5.   die(‘Could not connect: ‘ . mysql_error());  
  6.   }  
  7. mysql_select_db(“mysql”$con);  
  8. print 

    MySQL: Simple select statement

    ;  

  9. $result = mysql_query(“select * from emp_dtl”);    
  10. echo “
    ‘1’>  


  11.   

  12.   

  13.   

  14.   

  15.   

  16.   

  17. “;  

  18. while($row = mysql_fetch_array($result))  
  19.   {  
  20.    echo 
  21. ;  

  22.   echo 
  23. ;  

  24.   echo 
  25. ;  

  26.   echo 
  27. ;  

  28.   echo 
  29. ;  

  30.     echo 
  31. ;  

  32.   echo 
  33. ;  

  34.   }  
  35.   echo 
  36. EmpIdFirstnameLastnameRoleSalary
     . $row[‘id’] .  . $row[‘Firstname’] .  . $row[‘Lastname’] .  . $row[‘role’] .  . $row[‘salary’] . 

    ;  

  37.    
  38.     
  39.    
  40.   print 

    MySQL: Group by clause in PHP

    ;  

  41. $result = mysql_query(“select id, avg(salary)as totalsal from emp_dtl group by id”);   
  42. echo “
    ‘1’>  


  43.   

  44.   

  45.   

  46. “;  

  47. while($row = mysql_fetch_array($result))  
  48.   {  
  49.    echo 
  50. ;  

  51.   echo 
  52. ;  

  53.   echo 
  54. ;  

  55.   echo 
  56. ;  

  57.   }  
  58.   echo 
  59. EmpIdSalary
     . $row[‘id’] .  . $row[‘totalsal’] . 

    ;  

  60.    
  61.   print 

    MySQL: Group by with having clause

    ;  

  62. $result = mysql_query(“select id, count(*) as total from emp_dtl group by id having count(*)>1”);   
  63. echo “
    ‘1’>  


  64.   

  65.   

  66. ;  

  67. “;  

  68. while($row = mysql_fetch_array($result))  
  69.   {  
  70.    echo 
  71. ;  

  72.   echo 
  73. ;  

  74.   echo 
  75. ;  

  76.   echo 
  77. ;  

  78.   }  
  79.   mysql_close($con);  
  80.   ?>  
  81.   
  82.   echo 
  83. EmpIdDuplicate Records
     . $row[‘id’] .  . $row[‘total’] . 

    ;  

Note: In the above example, first the query “select * from emp_dtl” simply shows all the information of the emp_dtl table. And the second query “select id, avg (salary) as totalsal from emp_dtl group by id” is grouped by the id column data and the result is each group’s average total salary. And the third “query select id, count(*) as total from emp_dtl group by id having count(*)>1” counts the number of duplicate records.

Output

group-by-clause-in-php.jpg

Difference between Where clause and Having clause

 

The “Where” clause is called before rows are grouped. You cannot use aggregate functions with the “where” clause.

 

The “Having” clause is called after rows are grouped. In other words it is included with the final result. You can use aggregate functions with the “having” clause. 

Article Prepared by Ollala Corp

You might also like
Leave A Reply

Your email address will not be published.