I have two tables, A and B, that have the same structure (about 30+ fields). Is there a short, elegant way to join these tables and only select rows where one or more columns differ? I could certainly write some script that creates the query with all the column names but maybe there is an SQL-only solution.
To put it another way: Is there a short substitute to this:
SELECT *
FROM table_a a
JOIN table_b b ON a.pkey=b.pkey
WHERE a.col1 != b.col2
OR a.col2 != b.col2
OR a.col3 != b.col3 # .. repeat for 30 columns
Taking on data into account, there is no short way. Actually this is the only solid way to do it. One thing you might need to be careful with is proper comparison of NULL values in NULL-able columns. The query with
ORtends to be slow, not mentioning if it is on 30 columns.Also your query will not include records in
table_bthat do not have corresponding one intable_a. So ideally you would use aFULL JOIN.If you need to perform this operation often, then you could introduce some kind of additional data column that gets updated always when anything in the row changes. This could be as simple as the
TIMESTAMPcolumn which gets updated with the help ofUPDATE/INSERTtriggers. Then when you compare, you even have a knowledge of which record is more recent. But again, this is not a bullet proof solution.