Powered By Blogger

Friday, 28 October 2011

finding difference in data between two tables

mysql> show create table emp \G
*************************** 1. row ***************************
       Table: emp
Create Table: CREATE TABLE `emp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `sal` int(11) DEFAULT NULL,
  `job` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> show create table emp1 \G
*************************** 1. row ***************************
       Table: emp1
Create Table: CREATE TABLE `emp1` (
  `id` int(11) NOT NULL DEFAULT '0',
  `name` varchar(100) DEFAULT NULL,
  `sal` int(11) DEFAULT NULL,
  `job` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select * from emp;
+----+------+------+------+
| id | name | sal  | job  |
+----+------+------+------+
|  1 | a    | 1000 | a    |
|  2 | b    | 5000 | b    |
|  3 | c    | 6000 | c    |
|  4 | d    | 2000 | d    |
+----+------+------+------+
4 rows in set (0.00 sec)
mysql> select * from emp1;
+----+------+------+------+
| id | name | sal  | job  |
+----+------+------+------+
|  1 | a    | 1000 | a    |
|  2 | b    | 2000 | b    |
|  3 | c    | 6000 | c    |
|  4 | d    | 2000 | c    |
+----+------+------+------+
4 rows in set (0.00 sec)

### first method
### The final query output is as follows:

mysql> SELECT emp.*, emp1.* FROM emp NATURAL LEFT JOIN emp1 AS x JOIN emp1 ON emp.id = emp1.id WHERE x.id IS NULL;
+----+------+------+------+----+------+------+------+
| id | name | sal  | job  | id | name | sal  | job  |
+----+------+------+------+----+------+------+------+
|  2 | b    | 5000 | b    |  2 | b    | 2000 | b    |
|  4 | d    | 2000 | d    |  4 | d    | 2000 | c    |
+----+------+------+------+----+------+------+------+
2 rows in set (0.00 sec)

### second method
mysql> SELECT * FROM emp AS a INNER JOIN emp1 AS b ON a.id = b.id AND (a.name, a.sal, a.job) != (b.name, b.sal, b.job);
+----+------+------+------+----+------+------+------+
| id | name | sal  | job  | id | name | sal  | job  |
+----+------+------+------+----+------+------+------+
|  2 | b    | 5000 | b    |  2 | b    | 2000 | b    |
|  4 | d    | 2000 | d    |  4 | d    | 2000 | c    |
+----+------+------+------+----+------+------+------+
2 rows in set (0.00 sec)

### third method


DELIMITER $$
DROP PROCEDURE IF EXISTS test.comparetables ;
CREATE PROCEDURE comparetables (schema1 varchar(100), table1 varchar(100), schema2 varchar(100), table2 varchar(100), excludecol1 varchar(100) )
begin
DECLARE q1 varchar(4000);
DECLARE q2 varchar(4000);
DECLARE q3 varchar(4000);
select group_concat('a.', column_name) into q1 from information_Schema.columns where table_name = table1 and table_schema = schema1  and column_name not like excludecol1 ;
select group_concat('b.', column_name) into q2 from information_Schema.columns where table_name = table2 and table_schema =schema2  and column_name not like excludecol1;
set q3 =concat('select * from ', schema1 , '.', table1,  ' AS  a  inner join ' ,schema2, '.',  table2, ' as b on a.', excludecol1, '=b.' , excludecol1, ' and (' , q1 , ') != (' , q2 , ')' );
select q1;
select q2;
select q3 ;
set @q4 = q3 ;
#prepare stmt from @q4;
#execute stmt;
#drop prepare stmt;
end ;
$$
delimiter ;

### execute the stored proc
call comparetables('test','emp','test','emp3', 'id');



No comments:

Post a Comment