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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With