tags:

views:

29

answers:

2

I've looked through all the related questions, but none of them does exactly what I need. I have two identically-structured tables (id, VoucherNbr, BalanceInit) - one is from our current production system (tableA), and the other is from a client's legacy system (tableB). Table A has records a,b,c,d,e (for example), and table B has a,b,c,d,e,f,g. Table B will always have all the values that exist in table A, but also has extra rows. I need to return a resultset that contains all the rows in table B that don't exist in table A (f and g, using the example.) How can I do this?

EDIT:

TABLE A

ID | VoucherNbr         | BalanceInit
=============================================
1  | 1111111111111111   | 25.00
2  | 2222222222222222   | 50.00
3  | 3333333333333333   | 10.00

TABLE B

ID | VoucherNbr         | BalanceInit
=============================================
15 | 1111111111111111   | 25.00
17 | 1212121212121212   | 15.00
22 | 2222222222222222   | 50.00
34 | 3333333333333333   | 25.00
41 | 3232323232323232   | 75.00

What I need to get back is a resultset containing only the rows that don't exist in Table A, where it's the value in the VoucherNbr field that doesn't exist, not the ID field.

ID | VoucherNbr         | BalanceInit
=============================================
17 | 1212121212121212   | 15.00
41 | 3232323232323232   | 75.00
+1  A: 

Note: Assuming id is the PK for both tables.

SELECT B.* FROM TableB B 
LEFT OUTER JOIN TableA A
ON B.id = A.id
WHERE A.id IS NULL

EDIT : Based on further commments from OP, since Id's are different for the same rows in the two tables, assuming that the matching criteria is based on comparing the 2 column values

SELECT B.* FROM TableB B 
LEFT OUTER JOIN TableA A
ON B.VoucherNbr = A.VoucherNbr
AND B.BalanceInit = A.BalanceInit
WHERE A.VoucherNbr IS NULL 

EDIT2 : If only voucher number itself is sufficient, then balanceInit too can be discarded

SELECT B.* FROM TableB B 
LEFT OUTER JOIN TableA A
ON B.VoucherNbr = A.VoucherNbr
WHERE A.VoucherNbr IS NULL 
InSane
Well, that's sort of the problem. It IS the PK for both tables, but the rows may not be in the same order in each table. For example, in table A, value b might be id=2, whereas in table B, value b might be id=50. I'm guessing your query won't work if that's the case?
EmmyS
Thanks! The edited version worked as well; I had already accepted another answer while you were posting, so sorry you're not getting points for that.
EmmyS
+1  A: 

Some databases offer this feature directly using a SUBTRACT or MINUS operation. MySQL doesn't seem to have any such operation.

I would probably do this:

SELECT  B.id, B.voucherNbr, B.balanceInit FROM tableB B
   WHERE NOT EXISTS 
     (SELECT * FROM tableA A 
      WHERE A.voucherNbr = B.voucherNbr AND A.balanceInit = B.balanceInit)

However, here are a couple of other MySQL solutions (I googled them) you can test for speed:

SELECT  id, voucherNbr, balanceInit FROM tableB 
   WHERE (voucherNbr, balanceInit) NOT IN 
      (SELECT voucherNbr, balanceInit FROM tableA);

SELECT  id, voucherNbr, balanceInit
   FROM tableB LEFT JOIN tableA USING (voucherNbr, balanceInit)
      WHERE tableA.voucherNbr IS NULL

Honestly, I like the first googled solution better than my own if the execution time is similar or better.

Note: I've modified these from my original answer to reflect the fact that you state in a comment to another answer that the ids vary in the two tables.

Larry Lustig
Thanks, the second one worked for me.
EmmyS