我想查询下面的两个表,以获取新表中当前表中尚未包含名称或代码条目的行。
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
在这个例子中它应该返回
name code
usa 1
sql查询将归档此enter code here
答案 0 :(得分:1)
SELECT name, code
FROM new
WHERE name NOT IN (SELECT DISTINCT name FROM current)
AND code NOT IN (SELECT DISTINCT code FROM current);
不知道你正在使用什么样的SQL,所以这个查询可能会有所不同或根本不起作用。
编辑后:此查询将检查当前表中是否存在名称和代码。但是,它不会检查它们是否在同一记录中。
答案 1 :(得分:1)
SELECT name, code
FROM new n
WHERE NOT EXISTS(SELECT * FROM current c
WHERE c.name = n.name OR c.code = n.code)
答案 2 :(得分:0)
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
或者只是
select * from new as n where not exist (select 1 from current as c where c.code = n.code)
答案 3 :(得分:0)
select * from new where name not in (select name from current) and code not in (select code from current);
可以在mysql中完成这个技巧
答案 4 :(得分:0)
试试这个:
select * from new_table where name not in (select name from current_table) and
code not in (select code from current_table);