In PHP, what are the advantages and disadvantages of using UNION ALL to combine results from multiple tables for a query?

When using UNION ALL to combine results from multiple tables in PHP, the advantage is that it allows you to retrieve data from different tables in a single query. However, the disadvantage is that it can be less efficient than using JOINs, especially when dealing with large datasets or complex queries. It is important to consider the performance implications before using UNION ALL.

<?php
// Using UNION ALL to combine results from two tables
$query = "SELECT column1 FROM table1
          UNION ALL
          SELECT column2 FROM table2";

$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        // Process the results
    }
} else {
    echo "No results found";
}

mysqli_close($connection);
?>