diff between tables

China☆狼群 提交于 2019-12-11 08:14:55

问题


I have two tables, with the same structure, for example: table "first' with columns 'a','b','c' and table 'second' with the same columns. How to find difference betweet those two tables? Of course, I can make some script on python, that will make set(a)-set(b), but I think there is some way to do it in mysql.

UPD:

Table 'first'
a   |b   |c
====|====|====
a1  |b1  |c1
a2  |b2  |c2
a3  |b3  |c3

Table 'second'
a   |b   |c
====|====|====
a2  |b2  |c2
a3  |b3  |c3
a4  |b4  |c4

the result I need is something like that:

Table 'first-second'
a   |b   |c
====|====|====
a1  |b1  |c1

Or

Table 'second-first'
a   |b   |c
====|====|====
a4  |b4  |c4

回答1:


You could try an outer join. For example, you could find rows present in table first but absent in table second like this (not tested):

SELECT first.a, first.b, first.c FROM first LEFT JOIN second USING(a,b,c) 
WHERE second.a IS NULL

The join gives you a table containing all rows present in first, like this:

first.a first.b first.c second.a second.b second.c
   a1      b1     c1      NULL      NULL     NULL
   a2      b2     c2       a2       b2       c2

Now you only have to query for rows with second.a IS NULL to find rows absent in second.

Performance might be poor since you have to join over all columns.




回答2:


difference means? differenciate the fields while writing queries ??? u can use first.a, second.a etc while writing queries.! (hope i answered ur question, if not : throw more light on the question tat i understand it better)




回答3:


You want this:

select column_name
from information_schema.columns
where
    table_name = 'FirstTable'
    and table_schema = 'DatabaseHoldingFirstTable'
    and column_name not in (
        select column_name
        from information_schema.columns
        where table_name = 'SecondTable'
        and table_schema = 'DatabaseHoldingSecondTable'
    );


来源:https://stackoverflow.com/questions/2521513/diff-between-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!