从列中找到不匹配的记录(联盟)Sql

时间:2018-05-03 21:50:40

标签: mysql sql

我需要找到两列之间不匹配的记录。

我有这个问题:

select iNum 
from doc_file_fs 
where iNum not in 
(select iFile from doc_client_fs)
union (select iFile from doc_employee_fs)
union (select iFile from doc_prov_fs)
UNION (select  iFile from doc_soc_fs)
UNION (select  iFile from doc_cor_fs)

但只能从第一个选择中找到一个而不是其他联盟......

拜托,我需要帮助,我是新来的,如果问题形成不充分,请耐心等待:C

2 个答案:

答案 0 :(得分:2)

我强烈建议您像这样编写查询:

select iNum 
from doc_file_fs  df
where not exists (select 1 from doc_client_fs dc where df.iNum = dc.iFile) and 
      not exists (select 1 from doc_employee_fs de where df.iNum = de.iFile) and 
      not exists (select 1 from doc_prov_fs dp where df.iNum = dp.iFile) and 
      not exists (select 1 from doc_cor_fs ds where df.iNum = ds.iFile) and 
      not exists (select 1 from doc_employee_fs de where df.iNum = de.iFile) and 
      not exists (select 1 from doc_cor_fs dc where df.iNum = dc.iFile) ;

这可以利用每个其他表上ifile的索引。这应该比使用unionnot in的版本快得多。

答案 1 :(得分:1)

这会起作用吗?

SELECT iNum 
FROM doc_file_fs 
WHERE iNum NOT IN 
(SELECT iFile FROM doc_client_fs
UNION
SELECT iFile FROM doc_employee_fs
UNION
SELECT iFile FROM doc_prov_fs
UNION
SELECT iFile FROM doc_soc_fs
UNION
SELECT iFile FROM doc_cor_fs)

我认为您的问题在于将UNION置于括号内。