Tuesday, January 26, 2010

[Level 2] How to get the difference between two tables.

Two table have the same data in echo column, how to get the difference between two tables.
You can use out join and constraint the condition.

mysql> create table table1 (id int, name varchar(32));
mysql> create table table2 (id int, name varchar(32));
mysql> insert into table1 values(1,'Stanley'),(2,'John'),(4,'Mary');
mysql> insert into table2 values(1,'Stanley'),(3,'Joseph'),(5,'Christy');

case 1: find the data in table1 but not in table2;
mysql> select * from table1 left join table2 on table1.id=table2.id where table2.id is null;

case 2: find the data in table2 but not in table1;
mysql> select * from table2 left join table1 on table2.id=table1.id where table1.id is null;

Wish this helps.

Stanley Huang