简体   繁体   中英

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

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 :)

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
Select
    *
From
    Table a
join 
    Table2 b on a.msisdn = b.msisdn
where 
    (a.firstname != b.firstname) OR (a.lastname != b.lastname)

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 :)

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