0

I have two tables with identical definition:

create tabA (
user_id int,
contact boolean,
promote boolean
);

create tabB (
user_id int,
contact boolean,
promote boolean
);

I want to compare two columns contact and promote and see if there is any discrepancy in row data. For example:

row from tabA: 1,T,T
row from tabB: 1,T,F

So there is discrepancy now I want to catch that and select only those rows where they are not equal.

1
  • You would have to define what happens with NULL values, which are obviously allowed according to your table definitions ... Commented May 26, 2015 at 23:58

4 Answers 4

1
SELECT * FROM tabA, tabB 
WHERE tabA.user_id = tabA.user_id 
AND (
 tabA.contact != tabB.contact 
 OR
 tabA.promote != tabB.promote
);
Sign up to request clarification or add additional context in comments.

1 Comment

you got it !! appreciate your help
1

As long as there can be NULL values, you need to use null-safe operators:

SELECT user_id, a.contact AS a_contact, a.promote AS a_promote 
              , b.contact AS b_contact, b.promote AS b_promote 
FROM   tabA a
JOIN   tabB b USING (user_id)
WHERE  a.contact IS DISTINCT FROM b.contact OR
       a.promote IS DISTINCT FROM b.promote;

Comments

1

Another option is to use Postgres' record comparison capability:

select *
from taba a
  full join tabb b using (user_id)
where a is distinct from b;

Comments

0

To find differences in table content you should compare 3 column's values of next query:

select (
        select count(*) from (
            select * from a
            union
            select * from b
        ) m
    ) merged,
    (select count(*) from a) in_a,
    (select count(*) from b) in_b;

If value in merged column is not equal to value in columns in_a and in_b then the content of table a and b has at least one difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.