Professional Documents
Culture Documents
name
address
Robert John
123 Avenue, WS
robert@sitename.com
Elena Hick
567 avenue
elana@sitename.com
Greek Tor
987 street
greek@sitename.com
Marr Batson
marry@sitename.com
Don Rafel
456 Rafel
don@sitename.com
product_name
product_dtl
CPU
Keyboard
Mouse
Monitor
CD Drive
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
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.
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.
Table one ( t1 )
id
name1
one1
two1
three1
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)
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 ( 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
$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>";
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
Display all students with a mark for those who are selected
From the above list you can see by using WHERE condition we can filter out
our required records
SELECT * FROM `student` LEFT JOIN student_football ON id=f_id WHERE f_id IS NOT
NULL
SELECT * FROM `student` LEFT JOIN student_football ON id=f_id WHERE f_id IS NULL
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 )