SQL How to compare data in two tables and get the results that are different between two tables

冷暖自知 提交于 2019-12-11 04:23:15

问题


There are 2 tables. Table a and b. A contains msisdn, firstname, secondname, lastname, regdate(registration data). Table b also has the same fields. I want to compare these two tables, the msisdn's, firstname and lastname fields. If msisdn X in table A has firstname as jim and lastname as halpert, and the same msisdn X has firstname as michael and secondname as scott in table B, i need to get these kinds of msisdn's as my query result. the one's with same msisdn in both tables and different names. if either of these names(first or last) mismatches, that should be shown as result.

I'm sorry if i did not explain the scenario accurately. I hope someone understands and answers this.

thanks :)


回答1:


SELECT A.*, B.* 
FROM TABLEA A
INNER JOIN TABLEB B ON A.MSISDN = B.MSIDN
WHERE A.firstname != B.firstname 
OR A.lastname != B.Lastname



回答2:


Select
    *
From
    Table a
join 
    Table2 b on a.msisdn = b.msisdn
where 
    (a.firstname != b.firstname) OR (a.lastname != b.lastname)



回答3:


If your table is does not have any foreign key you can try this:

SELECT tableA.*, tableB.* 
FROM tableA, tableB
WHERE tableA.col1 != tableB.col1
OR tableA.col2 != tableB.col2

You can change the operator with any operator you want,

Maybe it does not look so pro but it's easier to me :)



来源:https://stackoverflow.com/questions/39353584/sql-how-to-compare-data-in-two-tables-and-get-the-results-that-are-different-bet

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