sql根据某一个字段重复只取第一条数据

sql根据某一个字段重复只取第一条数据,像我们这里 只考虑手机重复,要是手机重复,我们只取第一条记录。不考虑圈红色部分最后一个字段不一样,怎么解决?
哪位好心人帮忙下嘿
亲,其实这里是要针对Excel 表导入进行过滤的(Excel表导入到SQL数据库的表已经写在asp.net方法里辣),Excel表导入到sql中间表这里要过滤,其实也就是要写访问Excel的sql过滤语句,然后添加到sql表中

代码如下:

select * from tbl_DPImg where ID in (select min(ID) from tbl_DPImg group by DPID)

处理后结果为:


查找表中多余的重复记录,重复记录是根据单个字段(teamId)来判断

select * from team where teamId in (select teamId from team group by teamId having count(teamId) > 1) 

删除表中多余的重复记录,重复记录是根据单个字段(teamId)来判断,只留有rowid最小的记录 

delete from team where

teamName in(select teamName from team group by teamName having count(teamName) > 1) 

and teamId not in (select min(teamId) from team group by teamName having count(teamName)>1)

扩展资料

数据记录筛选:

sql="select * from 数据表 where字段名=字段值 order by字段名[desc]"(按某个字段值降序排列。默认升序ASC)

sql="select * from 数据表 where字段名like '%字段值%' order by 字段名 [desc]"

sql="select top 10 * from 数据表 where字段名=字段值 order by 字段名 [desc]"

sql="select top 10 * from 数据表 order by 字段名 [desc]"

sql="select * from 数据表 where字段名in ('值1','值2','值3')"

sql="select * from 数据表 where字段名between 值1 and 值2"

参考资料来源:百度百科:SQL语句大全

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-07

使用分析函数row_number() over (partiion by ... order by ...)来进行分组编号,然后取分组标号值为1的记录即可。目前主流的数据库都有支持分析函数,很好用。

其中,partition by 是指定按哪些字段进行分组,这些字段值相同的记录将在一起编号;order by则是指定在同一组中进行编号时是按照怎样的顺序。

示例(SQL Server 2005或以上适用):

select s.*  
from ( 
    select *, row_number() over (partition by [手机号] order by [店铺]) as group_idx  
    from table_name
) s
where s.group_idx = 1

追问

亲,你太厉害了!!!这些对于sql的表完全没有问题,但是要是用在Excel 表上会出错

追答

Excel好像不能支持的,它不是实际意义上的数据库。就连SQL Server也是2005开始有支持分析函数的。
如果是Excel那么要使用Excel的方式来解决。

追问

还是很感谢你!!!

本回答被提问者和网友采纳
第2个回答  2014-11-05
如果仅仅只是查询出来去从,那么就用distinct
select distinct 需要去重的列明(允许多列) from table

如果是需要在表中删除,可以这样处理
1、建立临时表,将重复记录查询出来去重插入到临时表
2、删除实表中的重复记录
3、将临时表中的记录插入到实表
处理完成
第3个回答  2014-11-05
select * into ##tmp_table from 表 where 1=2
declare @PhoneNO int
declare cur cursor for
select 手机号 from 表 group by 手机号
open cur
fetch next from cur into @PhoneNO
while @@fetch_status=0
begin
insert into ##tmp_table
select top 1 from 表 where 手机号=@PhoneNO
fetch next from cur into @PhoneNO
end

select * from ##tmp_table
drop table ##tmp_table追问

亲,这个有点难,技术水平有限

追答

总体思路就是 把每个 手机号取第一条数据放入临时表,最后查询临时表

第4个回答  2014-11-05
最简单的 select distinct (手机号)追问

但是我得整条数据啊

select distinct (手机号) 只能取到‘手机号’这个字段而已啊

追答

你!!!你在字段后面加其他字段呀,select distinct (手机号),电话 你想要什么字段就加多少字段

相似回答