Open In App

PHP – Mysql GROUP BY HAVING Clause

Problem Statement  :
In this article, we are going to perform database operations with GROUP BY HAVING operation through PHP through xampp server.

In this scenario, we are going to consider the food database.



Requirements :
xampp server

Introduction :
PHP is a server side scripting language that can communicate with Mysql web server through xampp tool.



MySQL is a query language that can communicate with php through xampp.

Syntax –

SELECT column1,column2,columnn, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2,columnn;

Syntax –

SELECT column1,column2,columnn 
FROM table_name
GROUP BY column_name
HAVING aggregate_function(column_name) condition;

Example Query:
Consider the food database:

Select food items with cost greater than 200

SELECT  food_item 
from food 
GROUP BY(food_item) 
HAVING SUM(cost) > 200;

Result:

Item : cakes
Item : chocoss
Item : fry
Item : milk

Query:

Food items with weight less than 100

SELECT food_item 
from food 
GROUP BY(food_item) 
HAVING SUM(weight)>100;

Result:

Item : cakes

Approach:

Steps :

Refer this for insert records into xampp :

https://www.geeksforgeeks.org/performing-database-operations-in-xampp/amp/

table columns-structure

After typing this code run it in tour web browser by typing “localhost/form.php”




<?php
    // code
?><html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "geek";
 
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "GROUP BY HAVING Demo "; echo"</h1>";
echo "<br>";
echo "food items with cost greater than 200";
echo "<br>";
echo "<br>";
//sql query
$sql = "SELECT  food_item from food GROUP BY(food_item) HAVING SUM(cost)>200";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
    echo " Item : ". $row['food_item'];
    echo "<br>";
     
}
 
 
echo "<br>";
echo "food items with weight less than 100";
echo "<br>";
echo "<br>";
//sql query
$sql = "SELECT  food_item from food GROUP BY(food_item) HAVING SUM(weight)>100";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
    echo " Item : ". $row['food_item'];
    echo "<br>";
     
}
//close the connection
 
$conn->close();
?>
</body>
</html>

Output:

Example-2:

Display food items with  average cost greater than 400




<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "geek";
 
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
echo "<h1>"; echo "GROUP BY HAVING Demo "; echo"</h1>";
echo "<br>";
echo "food items with  average cost greater than 400";
echo "<br>";
echo "<br>";
//sql query
$sql = "SELECT  food_item,food_id from food GROUP BY(food_item) HAVING AVG(cost)>400";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
    echo " Item : ". $row['food_item'], " ----   Item id : ". $row['food_id'];
    echo "<br>";
     
}
 
//close the connection
 
$conn->close();
?>
</body>
</html>

Output:


Article Tags :