inner join in php – How to Use Joins in PHP?

inner join in php – A join lets you combine columns from two or more tables into a single result or say a SQL statement that retrieves data from two tables is called a join.

Inner joins let us select rows that have same value in both tables for specified columns thereby returns matching rows.

inner join in php – inner join program code in PHP, MySQL

Inner join shows results from both tables where there is any match between columns in both tables. here you will learn to MySQL JOINS Tutorial: INNER, OUTER, LEFT, RIGHT, CROSS.

Free Live Chat for Any Issue

INNER JOIN

<?php
$link=mysql_connect("localhost","api_demo","pakainfo_v1");
if (!$link)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("Memberloyees", $link);
$qry="SELECT Member.id,Member.MemberNm, group.group_name FROM Member INNER JOIN group on Member.id = group.group_id";
$result=mysql_query($qry)ordie(mysql_error());
echo"<tableborder='1'>
<tr>
<th>MemberId</th>
<th>MemberNm</th>
<th>groupName</th>
</tr>";

while($row = mysql_fetch_array($result)){
	echo "<tr>";
	echo "<td>" . $row['id'] . "</td>";
	echo "<td>" . $row['MemberNm'] . "</td>";
	echo "<td>" . $row['group_name'] . "</td>";
	echo "</tr>";
  }

echo "</table>";

mysql_close($link);
?>

RIGHT OUTER JOIN

<?php
$link=mysql_connect("localhost","api_demo","pakainfo_v1");

if (!$link)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("Memberloyees", $link);
$qry="SELECT Member.id, Member.MemberNm,group.group_name from Member RIGHT OUTER JOIN group on Member.id=group.group_id";
$result=mysql_query($qry)ordie(mysql_error());

echo"<tableborder='1'>

<tr>
<th>MemberId</th>
<th>MemberNm</th>
<th>groupName</th>

</tr>";

while($row = mysql_fetch_array($result)){

	echo "<tr>";
	echo "<td>" . $row['id'] . "</td>";
	echo "<td>" . $row['MemberNm'] . "</td>";
	echo "<td>" . $row['group_name'] . "</td>";
	echo "</tr>";

  }

echo "</table>";

mysql_close($link);
?>

MySQL INNER JOIN clause

SELECT
    select_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;

SELF JOIN

<?php

$link=mysql_connect("localhost","api_demo","pakainfo_v1");
if (!$link){
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("Memberloyees", $link);

$qry="select a.team_name,b.team_name,a.team_city from Member a, Member b where a.team_city=b.team_city and a.team_name<>b.team_name";

$result=mysql_query($qry)ordie(mysql_error());
echo"<tableborder='1'>
<tr>
<th>MemberName</th>
<th>MemberCity</th>

</tr>";

while($row = mysql_fetch_array($result)){

	echo "<tr>";
	echo "<td>" . $row['team_name'] . "</td>";
	echo "<td>" . $row['team_city'] . "</td>";
	echo "</tr>";

}

echo "</table>";

mysql_close($link);
?> 

PHP SQL INNER JOIN Query On Multiple Tables

Also Read This πŸ‘‰   How to encode a PHP file with base64? - base64 encode php script

PHP SQL Inner Join

Source Code of innerJoin.php

<?php
  $link = mysql_connect("localhost","admin_v1","pakainfo_v1");
  if (!$link) {
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $link);
  $result = mysql_query("SELECT team.name, comment.comments 
   FROM team INNER JOIN comment ON team.team_id = comment.team_id");
  echo "<table border='1'>
  <tr>
  <th>Name</th>
  <th>Comments</th>
  </tr>";
  while ($row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['comments'] . "</td>";
  echo "</tr>";
  }
  echo "</table>";
  
  mysql_close($con);
?>

I hope you get an idea about inner join in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.