Join Tables With the Using Keyword in PHP – Info 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 with the “Using” . 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.  

 

 

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. $con=mysql_connect(“localhost”,“root”,“”);  
  2. if (!$con)  
  3.   {  
  4.   die(‘Could not connect: ‘ . mysql_error());  
  5.   }  
  6. mysql_select_db(“mysql”$con);  
  7. print 

    MySQL: Data of Emp table

    ;  

  8. $result = mysql_query(“select * from emp”);   
  9. echo “
    ‘1’>  


  10.   

  11.   

  12.   

  13.   

  14.   

  15. “;  

  16. while($row = mysql_fetch_array($result))  
  17.   {  
  18.    echo 
  19. ;  

  20.   echo 
  21. ;  

  22.   echo 
  23. ;  

  24.   echo 
  25. ;  

  26.   echo 
  27. ;  

  28.   echo 
  29. ;  

  30.   }  
  31.   echo 
  32. EmpIdFirstnameLastnameSalary
     . $row[‘id’] .  . $row[‘fname’] .  . $row[‘lname’] .  . $row[‘salary’] . 

    ;  

  33.   print 

    MySQL: Data of Designation table

    ;  

  34. $result = mysql_query(“select * from designation”);   
  35. echo “
    ‘1’>  


  36.   

  37.   

  38.   

  39. “;  

  40.   
  41. while($row = mysql_fetch_array($result))  
  42.   {  
  43.    echo 
  44. ;  

  45.   echo 
  46. ;  

  47.   echo 
  48. ;  

  49.   echo 
  50. ;  

  51.   }  
  52.   echo 
  53. EmpIdRole
     . $row[‘id’] .  . $row[‘role’] . 

    ;  

  54.  print 

    MySQL: Join with USING keyword

    ;  

  55. $result = mysql_query(“select fname,salary,role from emp CROSS JOIN designation USING(id) ORDER BY salary”);   
  56. echo “
    ‘1’>  


  57.   

  58.   

  59.   

  60. “;  

  61. while($row = mysql_fetch_array($result))  
  62.   {  
  63.    echo 
  64. ;  

  65.   echo 
  66. ;  

  67.    echo 
  68. ;  

  69.   echo 
  70. ;  

  71.   }  
  72.   echo 
  73. FirstnameRole
     . $row[‘fname’] .  . $row[‘role’] . 

    ;    

  74.   mysql_close($con);  
  75.   ?>  

Output

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

 

Article Prepared by Ollala Corp

You might also like
Leave A Reply

Your email address will not be published.