简体   繁体   中英

How do you compare two tables with the same columns but in a different order (SQL or python)?

I am trying to find out how to see if two table are the same in sql. The tables should be the exact same but the order of the columns is slightly different. My initial though is to subtract the two tables from eachother and verify that the result is 0 records but I do not think this will work because of the difference in the order of columns. Any suggestions? Your help is much appreciated,

Thank you,

John

If what you want is to compare tables you can try the following; minus and except depends on the SQL used by yout Data Base Management System.

select * from tableA
minus
select * from tableB

If no rows are return by query then data is the same for both tables,now this will work if both have same amount of rows.

When comparing different entries in two different columns size you will need to use

LEFT JOIN 

There is some source material you can use for reference:

Union! SQL Server

Compare two tables with different entries and column

Might not be the answer you are looking for, but you could create a view and select the columns in the matching order from there. Then the comparison should be easier.

You can perform a full outer join on each field, then compare the length of the result to the length both original tables.

SELECT * from 
Table_A FULL OUTER JOIN
Table_B on 
A.Field_X = B.Field_X
A.Field_Y = B.Field_Y

If the view returns the same number of results as the length of both original tables, then the two tables are the same for those fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM