简体   繁体   中英

sql find difference value between two tables

i have two tables table1 and table2. Table1 has data from the start of the year until today and Table2 has data from the start of the year until yesterday . There are difference value in a specific field(field1) from table1 and table2.

Example.

table 1 field1(5)
table 2 field1(3)

The row in tables its not new,it can be inserted a days earlier. I need to find the difference in the specific field that has **updated(changed)**not only inserted(new row).

Table1
------
John    | 3
George  | 2
Bill    | 1 
Table2
------
John    | 3
George  | 1
Bill    | 0

the result i want is from table1

George   | 2
Bill     | 1

I hope it helps.

Thank you very much

If not interested in new rows, you can use simply this:

select t.* from table1 t join table2 t2 
      on t.name = t2.name where t.value <> t2.value;

If you want also the new rows in t1, then:

select t.* from table1 t left join table2 t2 
    on t.name = t2.name 
    where t.value <> t2.value or t2.name is null;

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