Вы находитесь на странице: 1из 7

PHP Tutorial (Joins)

Joins Definitions
What is foreign key
How To set Foreign key
INNER join example
LEFT join query
RIGHT join query
FULL join query

Joins:
INNER JOIN: The Most Common Type of Join is INNER JOIN. It returns all the
rows from the multiple tables where the join condition met.
LEFT JOIN: it returns all rows from left table (table1) with matching rows in the
right table (table 2). The result is NULL on right side if there is no match
RIGHT JOIN: It returns all the rows from right table (table 2) with the matching
rows in left table (table1). The result is NULL on left side if there is no match.
FULL JOIN: Full outer joins returns all rows from left table (table1) and from
right table(table2)
FULL OUTER JOIN combines the result of left table and right table.

What is foreign key


Foreign key is a field (or collection of fields) in one table that uniquely
identifies a row of another table. In simpler words, the foreign key is defined
in a second table, but it refers to the primary key in the first table.

How to set foreign key


Go to PHPmyadmin
Change the type of table to InnoDB if already not set
View the structure of the table which will have a foreign key. Make the
referencing field an INDEX
Now come back to structure view and click Relation view.

SET UP the foreign key.


Click on save

INNER join with example


First of all I created two table in the data base (customer and customer_o)
Set up foreign key (customer_id) in table (customer_o).
Created two forms for data insertion in tables

Code for customer order form


<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">

<div id="left">
<label>Customer Orders</label>
</div>
<div id="right">
</div>
</div>
<center>
<div id="login-form">
<form method="post"action ="insertorder.php">
<table align="center" width="35%" border="0">
<tr>
<td> <input type="text" name="o_No" placeholder="Enter order
No" /></td>
</tr>
<tr>
<td> <input type="text" name="o_date" placeholder="Enter date"
/></td>
</tr>
<tr>
<td> <button><input type="submit" name="submit" value ="Enter
Record"></button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
Save this file with name order.php
Now for record insertion in data base I create new file with name
insertorder.php
Code for customer order insertion in database
<!-- insertion of data into database-->
<?php
include("dbconnection.php");
if($_POST['submit'])
{
$o_No=$_POST['o_No'];
$o_date=$_POST['o_date'];
if ($con==true )
{
//echo "true con";
mysqli_select_db($con,$mysql_database) or die("Could
not select database");
}
else{
echo "abc";
}

mysqli_query($con,"INSERT INTO
customer_o(customer_id,o_No ,o_date)VALUES('5','$o_No','$o_date')");
header("location:customer_order_detail.php");
mysqli_close($con);
}
?>
Save this file with name insertorder.php
Now to show customer detail record from database
Code for showing customer detail from database on webpage
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
include("dbconnection.php");
$selectSQL = 'SELECT
customer.customer_id,customer.customer_name,customer.contact_na
me,customer.city, customer.country,
customer_o.o_No,customer_o.o_date
FROM customer
INNER JOIN customer_o ON
customer.customer_id=customer_o.customer_id';
if( !( $selectRes = mysqli_query($con, $selectSQL) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().':
'.mysql_error();
}else{}
if( mysqli_num_rows( $selectRes )==0 ){
// echo '<tr><td colspan="4">No Rows Returned</td></tr>';
}else{
}
mysqli_close($con);
?>
<div id="header">
<div id="left">
<label>View Customer Detail</label>
</div>
<div id="right">
</div>
</div>
<table align="center" width="75%" border="1">
<thead>

<tr>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Contact Name</th>
<th>City</th>
<th>Country</th>
<th> Order Date</th>
<th> Order No</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_array( $selectRes ) ){
?>
<tr>
<td><?php echo $row['customer_id']; ?></td>
<td><?php echo $row['customer_name']; ?></td>
<td><?php echo $row['contact_name'];?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['country'];?></td>
<td><?php echo $row['o_date'];?></td>
<td><?php echo $row['o_No'];?></td>
</tr>
</tbody>
<?php } ?>
</table>
</body>
</html>

Save this file with name customer_order_detail.php

INNER JOIN Query


$selectSQL = 'SELECT
customer.customer_id,customer.customer_name,customer.conta
ct_name,customer.city, customer.country,
customer_o.o_No,customer_o.o_date FROM customer INNER
JOIN customer_o ON
customer.customer_id=customer_o.customer_id';

LEFT JOIN Query


$selectSQL = 'SELECT
customer.customer_name,customer.contact_name,customer.city,
customer.country FROM customer LEFT JOIN customer_o ON
customer.customer_id=customer_o.customer_id';

RIGHT JOIN Query


$selectSQL = 'SELECT
customer.customer_name,customer.contact_name,customer.city,
customer.country ,customer_o.o_No FROM customer RIGHT
JOIN customer_o ON
customer.customer_id=customer_o.customer_id';

FULL JOIN Query


$selectSQL = SELECT * FROM customer LEFT OUTER JOIN
customer_o ON customer.customer_id =customer_o.customer_id
UNION ALL
SELECT * FROM customer LEFT OUTER JOIN customer_o ON
customer.customer_id =customer_o.customer_id;

Вам также может понравиться