sql - using where clause with Union -


i have 2 tables, t1 , t2, identical columns(id, desc) , data. 1 of columns, desc, might have different data same primary key, id.

i want select rows these 2 tables such t1.desc != t2.desc

select a.id, b.desc  (select * t1       union        select * t2 b) a.desc != b.desc 

for example, if t1 has (1,'aaa') , (2,'bbb') , t2 has(1,'aaa') , (2,'bbb1') new table should have (2,'bbb') , (2,'bbb1')

however, not seem work. please let me know going wrong , right way right.

union all dumps rows of second part of query after rows produced first part of query. cannot compare a's fields b's, because belong different rows.

what trying locating records of t1 ids matching these of t2, different description. can achieved join:

select a.id, b.desc t1 join t2 b on a.id = b.id a.desc != b.desc 

this way records of t1 ids matching records of t2 end on same row of joined data, allowing comparison of descriptions inequality.

i want both rows selected descriptions not equal

you can use union all between 2 sets of rows obtained through join, tables switching places, this:

select a.id, b.desc -- t1 a, t2 b t1 join t2 b on a.id = b.id a.desc != b.desc     union select a.id, b.desc -- t1 b, t2 t2 join t1 b on a.id = b.id a.desc != b.desc 

Comments