Showing posts with label Inner Join. Show all posts
Showing posts with label Inner Join. Show all posts

Tuesday 27 November 2012

MySQL INNER JOIN

The INNER JOIN keyword returns rows when there is at least one match in both tables.

family Table:

PositionAge
Dad41
Mom35
Daughter17
Dog12

food Table:

MealPosition
RiceDad
DosaMom
Idly
EggDog

The INNER JOIN keyword returns rows when there is at least one match in both tables. If there are rows in "family" that do not have matches in "food", those rows will NOT be listed.
 
<?php 
/* Make a MySQL Connection */
$query = "SELECT family.Position, food.Meal FROM family INNER JOIN food ON 
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 "
";
}
?>
Output:-
Mom - Rice Dad - Dosa Dog - Egg