I have 2 databases test1 and test2. where I need to compare columns from schema1 to schema2 and fetch the results.If table in test1 schema not present on test2 schema ,it should ignore that table. I have below query which shows all the results including table not present in test2 schema.
select *
from (
select COALESCE(c1.table_name, c2.table_name) as table_name,
COALESCE(c1.column_name, c2.column_name) as table_column,
c1.column_name as test1,
c2.column_name as test2
from
(select table_name,
column_name
from information_schema.columns c
where c.table_schema ='test1') c1
right join
(select table_name,
column_name
from information_schema.columns c
where c.table_schema ='test2') c2
on c1.table_name = c2.table_name and c1.column_name = c2.column_name
union
select COALESCE(c1.table_name, c2.table_name) as table_name,
COALESCE(c1.column_name, c2.column_name) as table_column,
c1.column_name as test1,
c2.column_name as test2
from
(select table_name,
column_name
from information_schema.columns c
where c.table_schema ='test1') c1
left join
(select table_name,
column_name
from information_schema.columns c
where c.table_schema ='test2') c2
on c1.table_name = c2.table_name and c1.column_name = c2.column_name
) tmp
where test1 is null
or test2 is null
order by table_name,
table_column;
Is it possible to rewrite above query ,just compare table in test1 with test2 and display the mismatch columns.if table not present in test2 schema, it should ignore that table. Thanks