tags:

views:

78

answers:

5

I want to query two tables as below to get the rows in the new table that does not already have a name or code entry in the current table.

table current

name           code
japan          233
india          65
england        44


table new

name           code
japan          233
india          65
england-main   44
japan          2331
india          652
usa            1

In this exampe it should return

name         code
usa          1

what sql query will archive thisenter code here

+1  A: 
SELECT name, code 
FROM new 
WHERE name NOT IN (SELECT DISTINCT name FROM current)
AND code NOT IN (SELECT DISTINCT code FROM current);

No idea what flavour of SQL you're using though, so this query might differ or not work at all.

Post-edit: this query will check if the name and code exist in the current table. However, it does not check if they are in the same record.

Denis 'Alpheus' Čahuk
I tested in sqlite. I am going to implement in access. However this code returnsengland-proper 44usa 1It should not pick 'england-proper 44' because 44 already exists as a code value in current
veccy
Oh you're checking the code values as well? Hold on, will edit.
Denis 'Alpheus' Čahuk
A: 
select * from
(select 
    n.*,
    c.code as old_code
from 
    new as n left join 
    current as c on n.code = c.code) as T
where T.old_code is null

OR simply

select * from new as n where not exist (select 1 from current as c where c.code = n.code)
STO
A: 
select * from new where name not in (select name from current) and code not in (select code from current);

would do the trick in mysql

TooAngel
I am trying this
veccy
A: 

Try this:

select * from new_table where name not in (select name from current_table) and
code not in (select code from current_table);
Sarfraz
It needs to check name and code
veccy
@veccy: I have updated if you also want the code in there.
Sarfraz
This has worked as well in sqlite
veccy
A: 
SELECT name, code 
FROM new n
WHERE NOT EXISTS(SELECT * FROM current c 
                 WHERE c.name = n.name OR c.code = n.code)
Martin Smith
This has worked in sqlite. Thanks
veccy