Anda di halaman 1dari 10

SQL LINKING TABLE Command

Linking of table is a very common requirement in SQL. Different types of


data can be stored in different tables and based on the requirement the
tables can be linked to each other and the records can be displayed in a very
interactive way. Let us take one example of linking of tables by considering
product and customer relationship. We have a product table where all the
records of products are stored. Same way we will have customer table where
records of customers are stored. The daily sales keep the record of all the
sales. This sales table will keep record of which product who has purchased.
So linking is to be done from Sales table to product table and customer
table.
This is our customer table
customer_id

name

address

email

Robert John

123 Avenue, WS

robert@sitename.com

Elena Hick

567 avenue

elana@sitename.com

Greek Tor

987 street

greek@sitename.com

Marr Batson

456 Mary road

marry@sitename.com

Don Rafel

456 Rafel

don@sitename.com

Product table is below


product_id

product_name

product_dtl

CPU

CPU unit details here.

Keyboard

Keyboard Details available here

Mouse

Mouse details available here

Monitor

Monitor details here

CD Drive

CD Drive details here

Sales table is here below.


sales_id

product_id

customer_id

date_of_sale

2004-11-12 00:00:00

2004-01-11 00:00:00

2004-05-06 00:00:00

Related Tutorial

MySQL Left JoinMySQL INNER JoinMySQL Union

From these three tables let us find out the information on sales by linking
these tables. We will look into sales table and link it to the customer table
by the customer id field and in same way we will link product table by
product ID field. We will use WHERE sql command to link different tables.
Here is the command to do link three tables.

SELECT product_name, customer.name, date_of_sale FROM sales, product, customer


WHERE product.product_id = sales.product_id and customer.customer_id >=
sales.customer_id LIMIT 0, 30

The above SQL command links three tables and display the required result.
The tables are linked by their ID fields. The output is here
product_name

name

date_of_sale

CPU

Elena Hick

2004-11-12 00:00:00

Keyboard

Robort John

2004-01-11 00:00:00

Mouse

Robort John

2004-05-06 00:00:00

This way we can link three tables and get a meaningful report.
We may be interested to know which are the products not sold or who are
the customers who have not purchased today. We can prepare such reports
by using Left Join.

SQL left join query


We can link more than one table to get the records in different combinations as per
requirement. Keeping data of one area in one table and linking them each other
with key field is better way of designing tables than creating single table with more
number of fields. For example in a student database you can keep student contact
details in one table and its performance report in another table. You can link these
two tables by using on unique student identification number.
Here are two tables with some data. We will apply first sql where command to this
table

Table one ( t1 )
id

name1

one1

two1

three1

SELECT t1.id, t1.name1 FROM `t1`, t2 WHERE t1. id=t2.id

The output of the above sql command will give records of MySQL table t1 for which
there is a record present in table t2 and linking is done by id field.
id

name1

one1

two1

SELECT t1.id, name1, t2.id, t2.name2 FROM t1 left join t2 on t1.id = t2.id

id

name1

id

name2

one1

one1

two1

two2

three1

NULL

NULL

The above result shows a simple left join and its output. We may be interested to
identify the the records in table t1 for which there is no record present in table t2.
Here we have to use left join and link the tables by id field. Here is the query using
left join and the out put is below that.

SELECT t1.id, name1 FROM t1 left join t2 on t1.id = t2.id where ISNULL(t2.id)

for MySQL 5 and above try like this

SELECT t1.id, name1 FROM t1 left join t2 on t1.id = t2.id where (t2.id) is null

id

name1

three1

In the above result we have seen how the output works when we have to get
records which are not present in other table. Now let us find out the other way , we
will get the record of table one for which matching record exists in table 2. Here is
the left join sql and below that the results of the query.

SELECT t1.id,name1,t2.id,t2.name2 FROM t1 left join t2 on t1.id = t2.id where NOT


ISNULL( t2.id )

for MySQL 5 try like this

SELECT t1.id,name1,t2.id,t2.name2 FROM t1 left join t2 on t1.id = t2.id where ( t2.id ) NOT is
null

i nam i nam
d e1 d e2

Related

1 one1 1 one1

Affected rows

Tutorials

MySQL

INNER

JoinSQL

selectNumber

of

2 two1 2 two2

PHP script using left join to display records


We will try to develop one PHP script to display records by using left join query.
Here we have used PHP PDO connection to connect to MySQL database. You can
read more on PHP connection to MySQL using PDO here.
After connection we have used foreach loop to display all the records. Here is the
code.

<?Php

require "config.php"; // Database Connection details

$count="SELECT t1.id, name1 FROM t1 left join t2 on t1.id = t2.id where (t2.id) is null";

echo "<table>";

echo "<tr><th>id</th><th>name1</th></tr>";

foreach ($dbo->query($count) as $row) {

echo "<tr ><td>$row[id]</td><td>$row[name1]</td></tr>";

echo "</table>";

?>

We can replace the query part in above code to check other results.
Here is the SQL dump of two tables t1 & t2

Application of LEFT Join Query


We have two tables, on is student list and other one is selected football team. Some
of the students are selected to play for football team. In our football table we have
kept two student ids (selected for football team ).
Using left join we can list out various way to display the list.

Display all students with a mark for those who are selected

SELECT * FROM `student` LEFT JOIN student_football ON id=f_id

From the above list you can see by using WHERE condition we can filter out
our required records

List students who are selected for football team

SELECT * FROM `student` LEFT JOIN student_football ON id=f_id WHERE f_id IS NOT
NULL

Who are not selected for football team

SELECT * FROM `student` LEFT JOIN student_football ON id=f_id WHERE f_id IS NULL

Using three tables in LEFT Join Query


In above example we have seen how to find out records present in one table
and not present in another table. Let us try for another example.
In addition to football team there is one more baseball team. Two students
are selected for baseball team. So now we have two in football team and two
in baseball team.
Display all students with a mark for those who are selected for teams ( any )

SELECT * FROM `student`

LEFT JOIN student_football ON id=f_id

LEFT JOIN student_baseball on id=b_id

Next let us find out who are the students not selected in any of the team. In
other words find out the records in student table that don't have matching
records in football or baseball table ( team )

SELECT * FROM `student`

LEFT JOIN student_football ON id=f_id

LEFT JOIN student_baseball on id=b_id

WHERE f_id IS NULL and b_id is NULL

We can get similar result by using UNION , NOT IN and subqueries.

select product_name, customer.name, date_of_sale from customer,product,sales


where product.product_id = sales.product_id and customer.customer_id >=
sales.customer_id LIMIT 0, 30;

Anda mungkin juga menyukai