sql - I need to compare 2 tables in MySql and dispaly the differences -
both tables table_a , table_b has compared using employee_id column present in both of them. both tables have millions of rows. 3 results must displayed-
- employee_id present in table_a not present in table_b.
- vice-versa.
- there case when particular employee_id present in both tables data in other columns employee_id might not same in both tables. these rows must displayed showing columns there data mismatch.
since there millions of rows in both tables, process must fast both tables can compared quickly. using mysql server write query.
this rather tricky, here example assuming employee_id
unique in each table:
select employee_id, (case when max(which) = 'a' 'a-only' when min(which) = 'b' 'b-only' else 'both' end) which, concat_ws(',', (case when count(*) = 2 , not min(col1) <=> max(col1) 'col1' end), (case when count(*) = 2 , not min(col2) <=> max(col2) 'col2' end) ) differences ((select 'a' which, employee_id, col1, col2 ) union (select 'b' which, employee_id, col1, col2 b ) ) ab group employee_id;
note uses null
-safe comparison operator.
Comments
Post a Comment