PHP aggregates function

An aggregate function operates on a series of values and returns a single summary value, because an aggregate function typically operates on the values in columns and aggregate functions are also called column functions; they perform a calculation on the values in a set of selected rows and a QUERY that contains one or more aggregate functions is typically referred to as a summary query.

A. count():

$sql2 = "SELECT count(*) as total from ql_bids WHERE project_id = $projectId";
$results = $conn->query($sql2);

$data=mysqli_fetch_assoc($results);
echo $data['total'];

B. sum():

$sql2 = "SELECT sum(amount) as total from ql_bids WHERE project_id = $projectId";
$results = $conn->query($sql2);

$data=mysqli_fetch_assoc($results);
echo $data['total'];

C. max():

$sql2 = "SELECT max(amount) as total from ql_bids WHERE project_id = $projectId";
$results = $conn->query($sql2);

$data=mysqli_fetch_assoc($results);
echo $data['total'];

D. min():

$sql2 = "SELECT min(amount) as total from ql_bids WHERE project_id = $projectId"; 
$results = $conn->query($sql2); 
$data=mysqli_fetch_assoc($results); 
echo $data['total'];

D. avg():

$sql2 = "SELECT avg(amount) as total from ql_bids WHERE project_id = $projectId"; 
$results = $conn->query($sql2); 
$data=mysqli_fetch_assoc($results); 
echo $data['total'];

Leave a Reply