Menu bar

Monday 23 June 2014

Understanding Join Query.

Untitled Document Here we are goingto join two tables with common column named

Emp'Age' Table
EmpAge
Ron White32
Femina65
John25
Don

'Salary_Emp' Table
SalaryEmp
40000Ron White
29000Femina
25000
11000Ron White

The important thing to note here is that the column Emp contains information that can tie these two tables together. In the "Age" table, the Emp column contains all the Employee of the Company and their respective ages. In the "Salary_Emp" table the Emp column contains the respective Salary of the Employee. It's only through a shared column relationship such as this that tables can be joined together, so remember this when creating tables you wish to have interact with each other.
//php start
// Make a MySQL connection using your database credentials
// Construct our join query
$query = "SELECT Age.Emp, Salary_Emp.Salary ".
 "FROM Age, Salary_Emp".
 "WHERE Age.Emp = Salary_Emp.Emp";
  
$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['Emp']. " - ". $row['Salary'];
 echo "
";
}
//php ends
EmpSalary
Ron White40000
Femina29000
Ron White11000

No comments: