How can the DISTINCT keyword be utilized in PHP MySQL queries to ensure unique results when selecting data from multiple tables?
When selecting data from multiple tables in a MySQL query, the DISTINCT keyword can be used to ensure that only unique results are returned. This is useful when joining tables that may have duplicate records, as it eliminates those duplicates from the result set. By using DISTINCT, you can guarantee that each row in the output will be distinct.
$query = "SELECT DISTINCT column_name FROM table1 JOIN table2 ON table1.id = table2.id";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row['column_name'] . "<br>";
}
} else {
echo "No results found.";
}