The task of joining MySQL mentions destroying two or more tables in a table. This means that whatever you have learned so far can be applied even after applying everything, to join the table.
family Table:
Position | Age |
---|---|
Dad | 41 |
Mom | 45 |
Daughter | 17 |
Dog |
food Table:
Meal | Position |
---|---|
Steak | Dad |
Salad | Mom |
Spinach Soup | |
Tacos | Dad |
PHP and MySQL Code:
<?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT family.Position, food.Meal ".
"FROM family, food ".
"WHERE family.Position = food.Position";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['Position']. " - ". $row['Meal'];
echo "<br />";
}
?>
The statement "WHERE family.Position = food.Position" will restrict the results to the rows where the Position exists in both the "family" and "food" tables.
Display:
Dad - Steak
Mom - Salad
Dad - Tacos
Mom - Salad
Dad - Tacos
Those are the results of our PHP script. Let's analyze the tables to make sure we agree with these results.
Compare the Tables:
Position | Age |
---|---|
Dad | 41 |
Mom | 45 |
Daughter | 17 |
Dog |
Meal | Position |
---|---|
Steak | Dad |
Salad | Mom |
Spinach Soup | |
Tacos | Dad |
0 comments:
Post a Comment