How can DISTINCT be effectively used in a PHP MySQL query to handle multiple rows returned by LEFT JOIN?

When using a LEFT JOIN in a MySQL query, multiple rows can be returned for each record if there are multiple matching rows in the joined table. To handle this and ensure only unique records are returned, the DISTINCT keyword can be used in the query. This will eliminate duplicate rows and return only distinct records.

$query = "SELECT DISTINCT column_name FROM table1 LEFT JOIN table2 ON table1.id = table2.id";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        // Process each unique record
    }
}