How can PHP use aggregate functions like Avg() with a WHERE clause to calculate values excluding specific criteria in a database query?

When using aggregate functions like Avg() with a WHERE clause in a database query, you can exclude specific criteria by adding the condition to the WHERE clause. For example, if you want to calculate the average of a column excluding rows where a certain condition is met, you can specify that condition in the WHERE clause. This allows you to calculate values based on specific criteria in the database.

// Connect to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to calculate the average of a column excluding rows where a specific condition is met
$sql = "SELECT AVG(column_name) AS avg_value FROM table_name WHERE condition <> 'specific_criteria'";

// Execute the query
$result = $conn->query($sql);

// Fetch the result
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Average value: " . $row["avg_value"];
    }
} else {
    echo "0 results";
}

// Close the connection
$conn->close();