What are the advantages of using aggregate functions like MIN and MAX in PHP for retrieving specific records from a database table?
When retrieving specific records from a database table in PHP, using aggregate functions like MIN and MAX can help streamline the process by allowing you to easily find the minimum or maximum value of a specific column. This can be especially useful when you need to retrieve the oldest or newest record based on a date or timestamp column, or when you need to find the highest or lowest value in a numeric column.
// Retrieve the oldest record from a database table using MIN aggregate function
$query = "SELECT * FROM table_name WHERE date_column = (SELECT MIN(date_column) FROM table_name)";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
// Retrieve the highest value from a numeric column using MAX aggregate function
$query = "SELECT * FROM table_name WHERE numeric_column = (SELECT MAX(numeric_column) FROM table_name)";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);