How can CONCAT() function be used in SQL to concatenate values from different tables in a query?
To concatenate values from different tables in a query using the CONCAT() function in SQL, you can simply include the function in the SELECT statement and specify the columns or values you want to concatenate. This can be useful when you need to combine data from multiple tables into a single result set. Example PHP code snippet:
$query = "SELECT CONCAT(table1.column1, ' ', table2.column2) AS concatenated_value
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['concatenated_value'] . "<br>";
}
} else {
echo "No results found.";
}