I have two tables with ID and value columns.
I want to union these two tables but if ID exists in second table, I want to discard all same IDs in first table and retrieve only these IDs from second table. How can I create the query for this?
First table:
ID Value
100 1
100 2
101 3
102 4
Second table:
ID Value
100 5
100 6
100 7
102 5
The result I want to achieve:
ID Value
100 5
100 6
100 7
101 3
102 5
I tried to do as suggested but it still returns only values from table 1:
String selectQuery = "SELECT * FROM " + TABLE1_NAME
+" UNION ALL"
+" SELECT * FROM " + TABLE2_NAME
+" WHERE "+id+" NOT IN (SELECT "+id+" FROM "+TABLE2_NAME+ ")";