views:

106

answers:

3

I have two tables and I want to get all records from one table that are different from the records in second table.

Eg.: if we have four records in the first table like A,B,C,D and three records in the second table thats A,B,C then the answer of query should be D.

I have tried "EXCEPT" operator but it doesn't work fine. Kindly help me in writing correct query for the given problem.

+2  A: 

How about:

select * from TABLE_A where (COL,COL2,..) not in (select COL1,COL2,.. from TABLE_B) 
union all 
select * from TABLE_B where (COL1,COL2,..) not in (select COL1,COL2,.. from TABLE_A); 
modz0r
+2  A: 

If the first table is called TABLE_A, and the second table is called TABLE_B, in order to enhance the differences you can use the MINUS operand, both sides. This means:

SELECT * FROM TABLE_B
MINUS
SELECT * FROM TABLE_A;

The previous statement shows the records present in TABLE_B, but not present in TABLE_A.

Of course, you have to use the MINUS in the other side:

SELECT * FROM TABLE_A
MINUS
SELECT * FROM TABLE_B;

The previous statement shows the records present in TABLE_A, but not present in TABLE_B.

If you don't want to use MINUS, you can create two Oracle VIEWS with the previous statements.

The chicken in the kitchen
Probably he can't use Oracle views, given that he has tagged the question 'android' and 'sqlite'. But for the more general problem you are certainly right.
Jim Kiley
minus doesnt present in SQLite.I want to get this done in android.
Nemat
A: 

You can use the EXCEPT, it works as follows :

Table Test1 : A, B, C Table Test2 : A, B, C, D

select * from test1 except select * from test2 : Will return you the records which are there in test1 but not in test2. Result will be null.

select * from test2 except select * from test1 : Will return you the records which are present in test2 but not in test1. Result for this query will give you "D".

Karan
I have used this query but it doesn't give me the required answer.Here is my code:Cursor c=db.rawQuery("select * from "+table1+ " EXCEPT "+ "select * from "+table2 ,null)Actually both of my tables have similar records.It works fine for the first time only i.e. it gives 0 records.After that it starts giving all the records of the first table we can say as we have similar records in both tables.Where is the error I dont understand.
Nemat
Are there same attributes in both the tables i.e table1 and table2 ?
Karan
yes....but its just one case.we can have different attributes too.
Nemat
If you have different attributes then you can't use "select *", you need to use the similar attributes from both the tables.
Karan
Doesn't what I wrote in my answer work?I tried it few times with different tables, removing all records with the same ID, and it worked every time.You should try it.
modz0r