Join Tables With the Using Keyword in PHP

Introduction
 
I have also described in my previous article how to work with "inner" and "outer" joins. You can follow this link:
 
 
to read about "inner" and "outer" joins in PHP. Now in this article I will tell you how to join tables with the "Using" keyword. The "Using" keyword is used in place of the "On" keyword. So the question is, if we have the "On" keyword then why do we need the "Using" keyword? The answer of this question is, suppose you have more than one table, and you want to join them. Now we have two tables ("emp", "designation"), See following image.
 
The emp table consists of four columns (id, fname, lname and salary) and the designation table consists of two columns (id and role). Now you can see that both tables have a column named "id". When you code an "equi join", it's common for columns that are being compared to have the same name. For joins like these, you can simplify the query with the "Using" keyword. The following query image shows that the "Using" keyword can be used instead of the "On" keyword. In the following example we have joined two tables using their id's. And on the basis of the id column name, the emp role will be returned from the designation table.  

mysql-select-statement.jpg 
 
In the following query image, we have joined two tables using their ids. And on the basis of the id column name, his/her role is returned from the designation table.
 
The summary of the preceding explanation is that you can use the "Using" keyword instead of the "On" keyword when you want to join more than one table and the tables have the same column name, and you want to compare these  table's data using the same columns name. You can use the "Using" keyword.

mysql-join-with-using-keyword.jpg

Syntax
  1. SELECT select_list from table1  
  2.  [{LEFT/RIGHT} [OURT JOIN] tanbe2   
  3. USING (JOIN_Column1, JOIN_Column2, JOIN_Column3....................)  
  4. [{LEFT/RIGHT} [OURT JOIN]] table2  
  5. USING (JOIN_Column1, JOIN_Column2, JOIN_Column3....................)]............................  
In some cases, you may want multiple columns. To do that with the using clause, you can code multiple column names within parentheses, separating the columns name with commas.  It acts the same as "and" operators. Since the "Using" clause is more terse than the "On" clause, it can make your code easier to read and maintain.
 
Example of "Using" keyword in PHP
  1. <?php  
  2. $con=mysql_connect("localhost","root","");  
  3. if (!$con)  
  4.   {  
  5.   die('Could not connect: ' . mysql_error());  
  6.   }  
  7. mysql_select_db("mysql"$con);  
  8. print "<h2>MySQL: Data of Emp table</h2>";  
  9. $result = mysql_query("select * from emp");   
  10. echo "<table border='1'>  
  11. <tr>  
  12. <th>EmpId</th>  
  13. <th>Firstname</th>  
  14. <th>Lastname</th>  
  15. <th>Salary</th>  
  16. </tr>";  
  17. while($row = mysql_fetch_array($result))  
  18.   {  
  19.    echo "<tr>";  
  20.   echo "<td>" . $row['id'] . "</td>";  
  21.   echo "<td>" . $row['fname'] . "</td>";  
  22.   echo "<td>" . $row['lname'] . "</td>";  
  23.   echo "<td>" . $row['salary'] . "</td>";  
  24.   echo "</tr>";  
  25.   }  
  26.   echo "</table>";  
  27.   print "<h2>MySQL: Data of Designation table</h2>";  
  28. $result = mysql_query("select * from designation");   
  29. echo "<table border='1'>  
  30. <tr>  
  31. <th>EmpId</th>  
  32. <th>Role</th>  
  33. </tr>";  
  34.   
  35. while($row = mysql_fetch_array($result))  
  36.   {  
  37.    echo "<tr>";  
  38.   echo "<td>" . $row['id'] . "</td>";  
  39.   echo "<td>" . $row['role'] . "</td>";  
  40.   echo "</tr>";  
  41.   }  
  42.   echo "</table>";  
  43.  print "<h2>MySQL: Join with USING keyword</h2>";  
  44. $result = mysql_query("select fname,salary,role from emp CROSS JOIN designation USING(id) ORDER BY salary");   
  45. echo "<table border='1'>  
  46. <tr>  
  47. <th>Firstname</th>  
  48. <th>Role</th>  
  49. </tr>";  
  50. while($row = mysql_fetch_array($result))  
  51.   {  
  52.    echo "<tr>";  
  53.   echo "<td>" . $row['fname'] . "</td>";  
  54.    echo "<td>" . $row['role'] . "</td>";  
  55.   echo "</tr>";  
  56.   }  
  57.   echo "</table>";    
  58.   mysql_close($con);  
  59.   ?>  

Output

mysql-join-with-using-keyword-in-php.jpg

 

Up Next
    Ebook Download
    View all
    Learn
    View all