从SQL连接中删除重复项 [英] Removing duplicates from SQL Join

查看:110
本文介绍了从SQL连接中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个与我的真实问题接近的假设情况.表1

The following is a hypothetical situations this which is close to my real problem. Table1

recid   firstname    lastname   company
1       A             B          AAA
2       D             E          DEF
3       G             H          IJK
4       A             B          ABC

我有一个像这样的table2

I have a table2 which looks like this

recid   firstname    lastname   company
10      A             B          ABC
20      D             E          DEF
30      M             D          DIM
40      A             B          CCC

现在,如果我在recid上加入表,它将得到0结果,因为recid是唯一的,所以不会有重复.但是,如果我在firstname和lastname列上联接(这不是唯一的并且存在重复项),则在内部联接上会得到重复项.我在联接上添加的列越多,它变得越糟(创建了更多重复项).

Now if I join the table on recid, it will give 0 result, there will be no duplicates because recid is unique. But if I join on firstname and lastname column, which are not unique and there are duplicates, I get duplicates on inner join. The more columns I add on join, the worse it becomes (more duplicates are created).

在上述简单情况下,如何在以下查询中删除重复项.我想比较名字和姓氏,如果它们匹配,我将从table2返回名字,姓氏和Recid

In the above simple situation, how can I remove duplicates in the following query. I want to compare firstname and lastname, if they match, I return firstname, lastname and recid from table2

select distinct * from
(select recid, first, last from table1) a
inner join
(select recid, first,last from table2) b
on a.first = b.first


如果有人将来想玩它,就可以在这里找到脚本


Script is here if anyone wants to play with it in future

create table table1 (recid int not null primary key, first varchar(20), last varchar(20), company varchar(20))
create table table2 (recid int not null primary key, first varchar(20), last varchar(20), company varchar(20))

insert into table1 values(1,'A','B','ABC')
insert into table1 values(2,'D','E','DEF')
insert into table1 values(3,'M','N','MNO')
insert into table1 values(4,'A','B','ABC')

insert into table2 values(10,'A','B','ABC')
insert into table2 values(20,'D','E','DEF')
insert into table2 values(30,'Q','R','QRS')
insert into table2 values(40,'A','B','ABC')

推荐答案

您本身并不希望进行联接,您只是在测试是否存在/包含集合.

You don't want to do a join per se, you're merely testing for existence/set inclusion.

我不知道您正在使用哪种当前SQL风格,但这应该可以工作.

I don't know what current flavor of SQL you're coding in, but this should work.

SELECT MAX(recid), firstname, lastname 
FROM table2 T2
WHERE EXISTS (SELECT * FROM table1 WHERE firstname = T2.firstame AND lastname = T2.lastname)
GROUP BY lastname, firstname

如果要实现为联接,则使代码大致相同:

If you want to implement as a join, leaving the code largely the same:

SELECT max(t2.recid), t2.firstame, t2.lastname 
FROM Table2 T2 
INNER JOIN Table1 T1 
    ON T2.firstname = t1.firstname and t2.lastname = t1.lastname
GROUP BY t2.firstname, t2.lastname 

取决于DBMS,内部联接的实现方式可能不同于现有的(半联接与联接),但是优化器有时仍可以找出它并选择正确的运算符,而无论您以哪种方式编写它.

Depending on the DBMS, an inner join may be implemented differently to an Exists (semi-join vs join) but the optimizer can sometimes figure it out anyway and chose the correct operator regardless of which way you write it.

这篇关于从SQL连接中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆