SQL重复记录 [英] SQL duplicate records

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

问题描述

大家好,



我使用sql server 2005.我尝试使用多个表连接查询。我得到重复记录。相同的记录多次出现请给我解决方案

Hi to all,

I have using sql server 2005. i try left join query with multiple table.while i get duplicate records. same records occur multiple times please give me solution

推荐答案

使用Distinct关键字进行选择查询。



例如:



Make your select query with Distinct keyword.

For Example :

Select Distinct * from TableA Left Outer Join TableB on (TableA.FieldA = TableB.FieldB)


根据您的要求使用Distinct Or Group ..



use Distinct Or Group by according to your requirement..

create table tbl1
(
col1 int,
col2 varchar(10),
col3 varchar(10)
)
insert into tbl1 values(1,'aaa','bbb'),(2,'aaa','bbb'),(3,'bbb','ccc'),(4,'bbb','ccc'),(5,'ccc','xxx')


create table tbl2
(
col1 varchar(10),
col2 varchar(10),

)
insert into tbl2 values('aaa','hello'),('bbb','Hi')





----一般左查询---





----General left query---

select t1.col2,t1.col3,t2.col2 from tbl1 t1 
left join tbl2 t2
on t1.col2=t2.col1



结果:


result:

col2	col3	col2
aaa	bbb	hello
aaa	bbb	hello
bbb	ccc	Hi
bbb	ccc	Hi
ccc	xxx	NULL





----使用distinct ----



---- using distinct----

select distinct t1.col2,t1.col3,t2.col2
from tbl1 t1
left join tbl2 t2
on t1.col2=t2.col1</pre>



结果:


result:

col2	col3	col2
aaa	bbb	hello
bbb	ccc	Hi
ccc	xxx	NULL





---使用分组---



---Using Group by---

select t1.col2,t1.col3,t2.col2
from tbl1 t1
left join tbl2 t2
on t1.col2=t2.col1
group by t1.col2,t1.col3,t2.col2



结果:


Result:

col2	col3	col2
aaa	bbb	hello
bbb	ccc	Hi
ccc	xxx	NULL


请看看这里:查找和/或删除重复行 [ ^ ]


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

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