一个简单的去除重复字段的SQL查询语句
SQL去重的三种方法汇总
SQL去重的三种方法汇总在SQL中,我们经常需要进行去重操作,以去除重复的数据。
下面将介绍三种常用的去重方法。
方法一:使用DISTINCT关键字在SELECT语句中,可以使用DISTINCT关键字来去除重复的记录。
它会从结果中过滤掉具有相同值的记录,只返回唯一的记录。
例如,要从表中选择唯一的城市名称,可以使用以下语句:SELECT DISTINCT city FROM table_name;该语句将返回一个包含唯一城市名称的结果集。
使用DISTINCT关键字的优点是非常简单和直观,适用于不需要对其他列进行处理的简单去重操作。
然而,它仅适用于从结果集中选择唯一值的情况,不能对数据进行聚合或其他处理。
方法二:使用GROUPBY语句GROUPBY语句是一种更灵活的去重方法,它不仅可以去除重复的记录,还可以对数据进行聚合。
在SELECT语句中,可以使用GROUPBY子句将结果按照一个或多个列进行分组。
通过对分组列的聚合操作,可以对分组后的数据进行统计和计算。
例如,要对表中的城市进行去重,并统计每个城市的记录数量,可以使用以下语句:SELECT city, COUNT(*)FROM table_nameGROUP BY city;该语句将返回一个包含城市名称和对应记录数量的结果集。
使用GROUPBY语句的优点是可以对数据进行灵活的处理和统计,不仅能够进行去重操作,还可以进行聚合计算。
然而,由于需要对数据进行分组和聚合操作,语法相对复杂,性能也可能有所降低。
方法三:使用子查询另一种常用的去重方法是使用子查询。
子查询是嵌套在主查询中的查询语句,可以将子查询的结果作为主查询的条件或数据源。
在去重操作中,可以使用子查询来筛选出不重复的记录。
通过将DISTINCT关键字应用于子查询的结果中,可以得到不重复的记录。
例如,要从表中选择所有不重复的城市记录,可以使用以下语句:SELECT*FROM table_nameWHERE city IN (SELECT DISTINCT city FROM table_name);该语句将返回一个包含所有不重复城市记录的结果集。
SQL server去重
distinct去重将查询的出来的数据每一条都进行比较,只有完全相同在去重,效率低,速度慢,并且不能对text类型使用语法:select distinct 字段1,字段2,字段3 from 表;可以用在函数里面:语法:select count(distinct 字段1,字段2,字段3) from 表一、用group by作为条件去重例表studenty:要求:去除name为李四相同的数据用distinct去重(失败):Select distinct * from studenty;未去重成功,id是不一样的用group by去重(成功):根据指定的条件分组后只保存每组对应的一个id号select max(id) from studenty group by name,age如何根据id查询结果Select * from studenty where id in (select max(id) from studenty group by name,age)去重成功二、用ROW_NUMBER() OVER()排序函数去重(用法2)语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)解释:此函数是根据指定的字段进行分组,然后又根据另一个字段进行排序,得到一个排序的序号要求:根据name分组后按照score进行排序,需全部显现出来Select *,row_number()over(partition by name order by score) as 排序from studenty;结果:要求:只保留每个name的一条数据Select * from (select *,row_number()over(partition by name order by score) as排序from studenty) t where t.排序<=1;如果想保留两条数据则为where t.排序<3;以此类推结果:。
SQL查询去掉重复数据
SQL查询去掉重复数据本⽂主要总结数据库去掉重复数据的⽅法去掉重复数据的⽅法:第⼀种:distinct根据单个字段去重,能精确去重;作⽤在多个字段时,只有当这⼏个字段的完全相同时,才能去重;关键字distinct只能放在SQL语句中的第⼀个,才会起作⽤上图举例说明:图中student_name 为 test的同学有两位,不同的是班级⾸先,单个字段 -》⽤distinct对student_name 进⾏筛选,单个字段查询的话,可以看到已经将⼀个重复的test学⽣记录去掉了应⽤在多个字段时,可以看到此时两个同名的test,都被查出来;应⽤在多个字段时,只有当多个字段重复才会去重⼀般⽤来返回不重复的记录条数,返回不重复的条数(去掉test重复的,就剩下6条)第⼆种:group by + count + min 去掉重复数据没有加group by之前,有两条班级名称⼀样的数据加上group by 后,会将重复的数据去掉了count + group +min:去掉重复数据⾸先根据查出重复的数据然后再加上id不在查询结果⾥⾯的,去掉重复数据SELECT * from tb_class where classname in (SELECT classname from tb_class GROUP BY classname HAVING COUNT(classname)>1) and id NOT in (SELECT min(id) from tb_class GROUP BY classname HAVING count(classname)>1)第三种:min、max(这种⽅法在第⼆种中已经⽤到了)参考:https:///download/liangfei207/10325028https:///firstdream/p/7985584.html (较复杂)。
SQL去重的三种方法汇总
SQL去重的三种⽅法汇总
SQL去重的三种⽅法汇总
这⾥的去重是指:查询的时候, 不显⽰重复,并不是删除表中的重复项
1.distinct去重
注意的点:distinct
只能⼀列去重,当distinct后跟⼤于1个参数时,他们之间的关系是&&(逻辑与)关系,只有全部条件相同才会去重
弊端:当查询的字段⽐较多时,distinct会作⽤多个字段,导致去重条件增多
select distinct UserResult from Table1
2.group by去重
去重原理:将重复的⾏进⾏分组,相同的数据只显⽰第⼀⾏
弊端:使⽤group by后,所有查询字段都需要使⽤聚合函数,⽐较繁琐
select min(UserName)UserName,min(UserSex)UserSex,min(UserSubject)UserSubject,min(UserResult)UserResult from Table1 group by UserResult
3.row_number() over (parttion by 分组列 order by 排序列)
弊端:⼩孟还不知道
去重原理:现根据重复列进⾏分组,分组后再进⾏排序,不同的组序号为1,相同的组序号为2,排除为2的就达到了去重效果select *from
(
--查询出重复⾏
select *,row_number() over (partition by UserResult order by UserResult desc)num from Table1
)A
where A.num=1
这⾥安利第三个,row_number(),稳⼀些!。
使用SQL语句去掉重复的记录【两种方法】
使⽤SQL语句去掉重复的记录【两种⽅法】海量数据(百万以上),其中有些全部字段都相同,有些部分字段相同,怎样⾼效去除重复?如果要删除⼿机(mobilePhone),电话(officePhone),邮件(email)同时都相同的数据,以前⼀直使⽤这条语句进⾏去重:delete from 表 where id not in(select max(id) from 表 group by mobilePhone,officePhone,email )ordelete from 表 where id not in(select min(id) from 表 group by mobilePhone,officePhone,email )delete from 表 where id not in(select max(id) from 表 group by mobilePhone,officePhone,email )ordelete from 表 where id not in(select min(id) from 表 group by mobilePhone,officePhone,email )其中下⾯这条会稍快些。
上⾯这条数据对于100万以内的数据效率还可以,重复数1/5的情况下⼏分钟到⼏⼗分钟不等,但是如果数据量达到300万以上,效率骤降,如果重复数据再多点的话,常常会⼏⼗⼩时跑不完,有时候会锁表跑⼀夜都跑不完。
⽆奈只得重新寻找新的可⾏⽅法,今天终于有所收获://查询出唯⼀数据的ID,并把他们导⼊临时表tmp中select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email//查询出去重后的数据并插⼊finally表中insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)//查询出唯⼀数据的ID,并把他们导⼊临时表tmp中select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email//查询出去重后的数据并插⼊finally表中insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)效率对⽐:⽤delete⽅法对500万数据去重(1/2重复)约4⼩时。
sql语句去除重复记录(多表连接的查询)
sql语句去除重复记录(多表连接的查询)--处理表重复记录(查询和删除)/******************************************************************************************************************************************************1、Num、Name相同的重复值记录,没有⼤⼩关系只保留⼀条2、Name相同,ID有⼤⼩关系时,保留⼤或⼩其中⼀个记录******************************************************************************************************************************************************/--1、⽤于查询重复处理记录(如果列没有⼤⼩关系时2000⽤⽣成⾃增列和临时表处理,SQL2005⽤row_number函数处理)--> --> ⽣成測試數據if not object_id('Tempdb..#T') is nulldrop table#TGoCreate table#T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))Insert#Tselect1,N'A',N'A1'union allselect2,N'A',N'A2'union allselect3,N'A',N'A3'union allselect4,N'B',N'B1'union allselect5,N'B',N'B2'Go--I、Name相同ID最⼩的记录(推荐⽤1,2,3),⽅法3在SQl05时,效率⾼于1、2⽅法1:Select* from#T a where not exists(select1 from#T where Name= and ID<a.ID)⽅法2:select a.* from#T a join(select min(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID⽅法3:select* from#T a where ID=(select min(ID) from#T where Name=)⽅法4:select a.* from#T a join#T b on = and a.ID>=b.ID group by a.ID,,a.Memo having count(1)=1⽅法5:select* from#T a group by ID,Name,Memo having ID=(select min(ID)from#T where Name=)⽅法6:select* from#T a where(select count(1) from#T where Name= and ID<a.ID)=0⽅法7:select* from#T a where ID=(select top1 ID from#T where Name= order by ID)⽅法8:select* from#T a where ID!>all(select ID from#T where Name=)⽅法9(注:ID为唯⼀时可⽤):select* from#T a where ID in(select min(ID) from#T group by Name)--SQL2005:⽅法10:select ID,Name,Memo from(select*,min(ID)over(partition by Name) as MinID from#T a)T where ID=MinID⽅法11:select ID,Name,Memo from(select*,row_number()over(partition by Name order by ID) as MinID from#T a)T where MinID=1⽣成结果:/*ID Name Memo----------- ---- ----1 A A14 B B1(2 ⾏受影响)*/--II、Name相同ID最⼤的记录,与min相反:⽅法1:Select* from#T a where not exists(select1 from#T where Name= and ID>a.ID)⽅法2:select a.* from#T a join(select max(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID order by ID⽅法3:select* from#T a where ID=(select max(ID) from#T where Name=) order by ID⽅法4:select a.* from#T a join#T b on = and a.ID<=b.ID group by a.ID,,a.Memo having count(1)=1⽅法5:select* from#T a group by ID,Name,Memo having ID=(select max(ID)from#T where Name=)⽅法6:select* from#T a where(select count(1) from#T where Name= and ID>a.ID)=0⽅法7:select* from#T a where ID=(select top1 ID from#T where Name= order by ID desc)⽅法8:select* from#T a where ID!<all(select ID from#T where Name=)⽅法9(注:ID为唯⼀时可⽤):select* from#T a where ID in(select max(ID) from#T group by Name)--SQL2005:⽅法10:select ID,Name,Memo from(select*,max(ID)over(partition by Name) as MinID from#T a)T where ID=MinID⽅法11:select ID,Name,Memo from(select*,row_number()over(partition by Name order by ID desc) as MinID from#T a)T where MinID=1⽣成结果2:/*ID Name Memo----------- ---- ----3 A A35 B B2(2 ⾏受影响)*/--2、删除重复记录有⼤⼩关系时,保留⼤或⼩其中⼀个记录--> --> ⽣成測試數據if not object_id('Tempdb..#T') is nulldrop table#TGoCreate table#T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))Insert#Tselect1,N'A',N'A1'union allselect2,N'A',N'A2'union allselect3,N'A',N'A3'union allselect4,N'B',N'B1'union allselect5,N'B',N'B2'Go--I、Name相同ID最⼩的记录(推荐⽤1,2,3),保留最⼩⼀条⽅法1:delete a from#T a where exists(select1 from#T where Name= and ID<a.ID)⽅法2:delete a from#T a left join(select min(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID where b.Id is null⽅法3:delete a from#T a where ID not in(select min(ID) from#T where Name=)⽅法4(注:ID为唯⼀时可⽤):delete a from#T a where ID not in(select min(ID)from#T group by Name)⽅法5:delete a from#T a where(select count(1) from#T where Name= and ID<a.ID)>0⽅法6:delete a from#T a where ID<>(select top1 ID from#T where Name= order by ID)⽅法7:delete a from#T a where ID>any(select ID from#T where Name=)select* from#T⽣成结果:/*ID Name Memo----------- ---- ----1 A A14 B B1(2 ⾏受影响)*/--II、Name相同ID保留最⼤的⼀条记录:⽅法1:delete a from#T a where exists(select1 from#T where Name= and ID>a.ID)⽅法2:delete a from#T a left join(select max(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID where b.Id is null⽅法3:delete a from#T a where ID not in(select max(ID) from#T where Name=)⽅法4(注:ID为唯⼀时可⽤):delete a from#T a where ID not in(select max(ID)from#T group by Name)⽅法5:delete a from#T a where(select count(1) from#T where Name= and ID>a.ID)>0⽅法6:delete a from#T a where ID<>(select top1 ID from#T where Name= order by ID desc)⽅法7:delete a from#T a where ID<any(select ID from#T where Name=)select* from#T/*ID Name Memo----------- ---- ----3 A A35 B B2(2 ⾏受影响)*/--3、删除重复记录没有⼤⼩关系时,处理重复值--> --> ⽣成測試數據if not object_id('Tempdb..#T') is nulldrop table#TGoCreate table#T([Num] int,[Name] nvarchar(1))Insert#Tselect1,N'A'union allselect1,N'A'union allselect1,N'A'union allselect2,N'B'union allselect2,N'B'Go⽅法1:if object_id('Tempdb..#') is not nulldrop table#Select distinct* into# from#T--排除重复记录结果集⽣成临时表#truncate table#T--清空表insert#T select* from# --把临时表#插⼊到表#T中--查看结果select* from#T/*Num Name----------- ----1 A2 B(2 ⾏受影响)*/--重新执⾏测试数据后⽤⽅法2⽅法2:alter table#T add ID int identity--新增标识列godelete a from#T a where exists(select1 from#T where Num=a.Num and Name= and ID>a.ID)--只保留⼀条记录goalter table#T drop column ID--删除标识列--查看结果select* from#T/*Num Name----------- ----1 A2 B(2 ⾏受影响)*/--重新执⾏测试数据后⽤⽅法3⽅法3:declare Roy_Cursor cursor local forselect count(1)-1,Num,Name from#T group by Num,Name having count(1)>1declare@con int,@Num int,@Name nvarchar(1)open Roy_Cursorfetch next from Roy_Cursor into@con,@Num,@Namewhile @@Fetch_status=0beginset rowcount @con;delete#T where Num=@Num and Name=@Nameset rowcount 0;fetch next from Roy_Cursor into@con,@Num,@Nameendclose Roy_Cursordeallocate Roy_Cursor--查看结果select* from#T /*Num Name ----------- ----1 A2 B(2 ⾏受影响)*/。
删除数据库中重复数据的几个方法
删除数据库中重复数据的几个方法1.使用DISTINCT关键字查询重复数据:DISTINCT关键字可以用于从查询结果中去除重复的数据。
例如,可以使用以下SQL语句查询数据库中的重复数据,并删除重复的数据:```sqlDELETE FROM your_tableWHERE id NOT INSELECT MIN(id)FROM your_tableGROUP BY column_name```这个SQL语句将删除你的表中除了具有最小id的记录之外的所有重复记录。
2.使用临时表删除重复数据:另一种常用的方法是使用临时表将重复数据存储起来,然后再根据需要删除重复数据。
以下是一个示例:```sqlCREATE TABLE temp_table LIKE your_table;INSERT INTO temp_tableSELECTDISTINCT*FROM your_table;DELETE FROM your_table;INSERT INTO your_tableSELECT*FROM temp_table;DROP TABLE temp_table;```这个方法首先创建一个与你的表相同结构的临时表,然后使用DISTINCT关键字将去除重复的数据插入到临时表中。
接下来,删除你的表中的所有数据,并将临时表中的数据重新插入到你的表中。
最后,删除临时表。
3.使用GROUPBY和HAVING子句删除重复数据:GROUPBY和HAVING子句可以用于根据指定的列对数据进行分组,并删除重复分组。
以下是一个示例:```sqlDELETE FROM your_tableWHERE id NOT INSELECT MIN(id)FROM your_tableGROUP BY column_nameHAVINGCOUNT(*)>1```这个SQL语句将删除你的表中重复分组的记录,只保留每个分组中具有最小id的记录。
select消除重复记录的方法
select消除重复记录的方法在关系型数据库中,常常会遇到需要查询并消除重复记录的情况。
重复记录可能是由于数据录入错误、多次插入相同数据或者其他原因导致的。
为了保证查询结果的准确性和一致性,我们需要使用SELECT语句来消除重复记录。
一、使用DISTINCT关键字DISTINCT关键字用于查询不重复的记录,它可以应用于SELECT语句的列名或者使用*来表示所有列。
以下是使用DISTINCT关键字消除重复记录的语法:SELECT DISTINCT column1, column2, ...FROM table_name;其中,column1, column2, ...是需要查询的列名,table_name是需要查询的表名。
使用DISTINCT关键字时,查询结果将只返回不重复的记录。
例如,我们有一个名为employees的表,其中包含了员工的姓名和所在部门,我们可以使用以下语句查询不重复的部门名称:SELECT DISTINCT departmentFROM employees;二、使用GROUP BY子句除了使用DISTINCT关键字,我们还可以使用GROUP BY子句来消除重复记录。
GROUP BY子句将查询结果按照指定的列进行分组,并对每个分组进行计算或者聚合操作。
以下是使用GROUP BY子句消除重复记录的语法:SELECT column1, column2, ...FROM table_nameGROUP BY column1, column2, ...;其中,column1, column2, ...是需要分组的列名,table_name是需要查询的表名。
使用GROUP BY子句时,查询结果将只返回每个分组的一个记录。
例如,我们有一个名为orders的表,其中包含了订单的编号和客户名称,我们可以使用以下语句查询每个客户的订单数量:SELECT customer, COUNT(order_id) AS order_countFROM ordersGROUP BY customer;三、使用HAVING子句在使用GROUP BY子句进行分组查询时,有时我们需要对分组结果进行进一步的筛选,这时可以使用HAVING子句。
在MySQL中处理重复数据的方法
在MySQL中处理重复数据的方法引言:在日常的数据库操作中,我们经常会遇到处理重复数据的情况。
重复数据不仅占据了数据库的存储空间,还会对数据的查询和分析产生干扰,因此,合理有效地处理重复数据是数据库管理的一项关键任务。
本文将介绍在MySQL中处理重复数据的几种方法,旨在帮助读者更好地管理和优化数据库。
一、数据去重数据去重是最常见的处理重复数据的方法,它可以帮助我们删除重复的数据,从而提高数据库的性能和效率。
在MySQL中,我们可以通过以下几种方式进行数据去重。
1. 使用DISTINCT关键字DISTINCT关键字可以用于查询语句中,它可以去除查询结果中重复的行。
例如,我们有一个表格名为users,其中包含了用户的姓名和年龄两个字段。
如果我们想查询出所有不重复的用户姓名,可以使用以下SQL语句:```SELECT DISTINCT name FROM users;```这样就可以得到所有不重复的用户姓名。
2. 创建唯一索引索引是数据库中非常重要的数据结构,它可以提高数据库的查询效率。
我们可以通过在表格的字段上创建唯一索引来防止重复的数据被插入到数据库中。
例如,我们有一个表格名为products,其中的字段product_id是唯一标识每个产品的。
如果我们想防止相同的产品被插入到数据库中,可以在product_id字段上创建唯一索引,方法如下:```CREATE UNIQUE INDEX idx_product_id ON products (product_id);```这样,在插入数据时,如果发现要插入的product_id已经存在,MySQL就会报错。
3. 使用临时表在某些情况下,我们需要将一张表中的重复数据插入到另一张表中,并且在插入的过程中去除重复数据。
这时,我们可以使用临时表来处理重复数据。
临时表是一种专门用于存储临时数据的表格,它在数据处理完成后会自动被删除。
我们可以通过以下步骤使用临时表去除重复数据:1)创建一个临时表temp,结构和要去重的表一致。
SQL中删除重复记录
SQL中删除重复记录在SQL中删除重复记录是一个常见的操作任务。
重复记录可以在数据表中出现多次,这可能是由于错误的数据导入、重复的插入操作或其他数据更新问题导致的。
删除重复记录可以提高数据的准确性和一致性,从而保证数据的完整性。
下面是一些常见的方法,用于在SQL中删除重复记录。
1.使用DISTINCT关键字:DISTINCT关键字可以用于SELECT语句,以消除查询结果中的重复记录。
通过使用SELECTDISTINCT语句,我们可以获取没有重复记录的结果集。
然后,我们可以将这个结果集插入到新的表中,并删除原始表中的重复记录。
```--创建新表CREATE TABLE new_employeesSELECT DISTINCT name, phoneFROM employees;--删除原始表DROP TABLE employees;--重命名新表ALTER TABLE new_employeesRENAME TO employees;```2.使用ROW_NUMBER(函数和临时表:ROW_NUMBER(函数可以分配唯一的行号给查询结果集中的每一行。
我们可以将这个函数与临时表结合使用,以生成只包含非重复记录的结果集。
然后,我们可以使用新结果集创建一个新表,并删除原始表中的重复记录。
以下示例演示了如何使用ROW_NUMBER(函数和临时表删除重复记录:```--创建临时表CREATE TABLE tmp_employees ASSELECT *, ROW_NUMBER( OVER (PARTITION BY phone ORDER BY name) AS rnFROM employees;--创建新表CREATE TABLE new_employeesSELECT name, phoneFROM tmp_employeesWHERE rn = 1;--删除原始表DROP TABLE employees;--重命名新表ALTER TABLE new_employeesRENAME TO employees;--删除临时表DROP TABLE tmp_employees;```3.使用GROUPBY和HAVING子句:GROUPBY子句用于将查询结果按照指定的列进行分组。
sql删除重复数据方法
sql删除重复数据方法在SQL中,可以使用多种方法删除重复数据。
以下是一些常见的方法:1.使用DISTINCT关键字:DISTINCT关键字用于从结果集中仅选择唯一的值。
可以将DISTINCT用于SELECT语句,以选择指定列中的唯一值。
例如,如果要删除表中name列中的重复数据,可以使用以下语句:```DELETE FROM table_name WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP BY name);```这将删除除具有最小ID值的记录外,表中所有name列的重复记录。
2.使用临时表:可以使用临时表来删除重复数据。
首先,创建一个临时表,将表中的数据插入到临时表中。
然后,从临时表中删除重复数据。
最后,将临时表中的数据重新插入到原始表中。
以下是一个使用临时表删除重复数据的例子:```--创建临时表CREATE TABLE temp_table AS SELECT DISTINCT * FROMoriginal_table;--清空原始表TRUNCATE TABLE original_table;--从临时表中重新插入数据到原始表INSERT INTO original_table SELECT * FROM temp_table;```请注意,上述示例省略了保持表结构的细节。
3.使用ROW_NUMBER(函数:ROW_NUMBER(函数为每个行分配一个唯一的数字。
可以使用该函数删除重复数据。
以下是一个使用ROW_NUMBER(函数删除重复数据的例子:```DELETEFROMSELECT column1, column2, column3, ROW_NUMBER( OVER (PARTITION BY column1, column2, column3 ORDER BY (SELECT 0)) AS rnFROM table_nametWHERE t.rn > 1;```上述示例按列column1、column2和column3进行分组,并按指定的顺序为每个组分配唯一的行号。
使用SQL语句对重复记录查询、统计重复次数、删除重复数据
使用SQL语句对重复记录查询、统计重复次数、删除重复数据
上周工作中数据库中出现了N多重复记录的情况,导致联合查询时数据异常。
由于数据是客户提供的,当时并没有提供唯一标识列,而且也没预料到会出现重复数据。
哎,大意了。
后来对表中的数据进行了一次重复查询。
1、查找表中重复记录,重复记录是根据单个字段来判断,并统计重复次数
select [字段],count(0) AS 重复次数 from[表名] group by [字段] having count([字段]) > 1
2、删除表中多余的重复记录,重复记录是根据单个字段来判断,只留有rowid最小的记录
delete from [表名] where [字段] in ( select [字段] from [表名] group by [字段] having count([字段]) > 1 )
and rowid not in ( select min(rowid) from [表名] group by [字段] having count([字段]) > 1 )
3、查询无重复记录,根据单个字段查询
select distinct [字段] from [表名] order by [字段] desc
PS:血淋淋的教训,不管别人提供的数据里面否真的都是唯一的,一定要唯一主键或标识列(就算有重复的数据也不怕)。
操作的时候都已唯一主键或标识列来联合,要合理的使用本表主键和对方提供的唯一主键进行操作。
sql语句去重
sql语句去重1. distinct表userinfo 数据如下:id name age height10xiaogang2318111xiaoli3117612xiaohei2215213xiaogang2617214xiaoming31176现在需要当前用户表不重复的用户名select distinct name from userinfo如结果(1):name xiaogangxiaoheixiaolixiaoming可是我现在又想得到Id的值,改动如下select distinct name,id from userinfo如结果(2)xiaogang 10xiaoli 11xiaohei 12xiaogang 13xiaoming 14此时distinct同时作用了两个字段,即必须得id与name都相同的才会被排除2. group byselect namefrom userinfogroub by name运行上面3行sql的结果如上面distinct中的结果(1)select name,idfrom userinfogroub by name ,id运行上面3行sql的结果如上面distinct的结果(2)3. row_number() overSQL Server 通过Row_Number 函数给数据库表的记录进行标号,在使用的时候后面会跟over 子句,而over 子句主要用来对表中的记录进行分组和排序的。
语法如下:ROW_NUMBER() OVER(PARTITION BY COLUMN1 ORDER BY COLUMN2)1:Partition BY 用来分组2:Order by 用来排序接下来用 row_number() over 进行去重。
首先用name 进行分组,id进行排序。
具体SQL 语句如下SELECT * FROM (select *,ROW_NUMBER() over(partition by name order by id desc) AS rn from userinfo ) AS u WHERE u.rn=1结果如下id name age height rn13 xiaogang 26 172 112 xiaohei 22 152 111 xiaoli 31 176 114 xiaoming 31 176 1通过使用 row_number over 子句就能将所有的列展示出来,同时进行去重。
一次SQL如何查重及去重的实战记录
⼀次SQL如何查重及去重的实战记录⽬录前⾔1.distinct2.groupby3.row_number窗⼝函数4.删除重复数据第⼀步:找出重复的数据第⼆步:删除重复的数据总结前⾔在使⽤SQL提数的时候,常会遇到表内有重复值的时候,就需要做去重,本⽂归类了常⽤⽅法。
1.distinct题⽬:现在运营需要查看⽤户来⾃于哪些学校,请从⽤户信息表中取出学校的去重数据⽰例:user_profilemysql>SELECT DISTINCT university FROM user_profile;根据⽰例,查询返回以下结果⼩贴⼠:SQL中关键词distinct去重:英语中distinct 代表独⼀⽆⼆的意思,他在SQL表⽰去重的意思:⽐如本题中university这⼀列出现了两次北京⼤学,使⽤distinct进⾏去重查询后,则北京⼤学只出现⼀次。
distinct 通常效率较低distinct 使⽤中,放在 select 后边,对后⾯所有的字段的值统⼀进⾏去重拓展:题⽬:现在运营需要查看⽤户的总数select count(distinct university) from user_profile;2.group by举个栗⼦,现有这样⼀张表 task备注:task_id: 任务id;order_id: 订单id;start_time: 开始时间注意:⼀个任务对应多条订单题⽬:列出任务总数根据⽰例,查询⽅法如下:第1步:列出 task_id 的所有唯⼀值(去重后的记录,null也是值)select task_idfrom Taskgroup by task_id;第⼆步:任务总数select count(task_id) task_numfrom (select task_idfrom Taskgroup by task_id) tmp;3.row_number 窗⼝函数举个栗⼦,现有这样⼀张表 task备注:task_id: 任务id;order_id: 订单id;start_time: 开始时间注意:⼀个任务对应多条订单题⽬:查询整个表重复的数据根据⽰例,查询⽅法如下:– 在⽀持窗⼝函数的 sql 中使⽤select count(case when rn=1 then task_id else null end) task_numfrom (select task_id, row_number() over (partition by task_id order by start_time) rnfrom Task) tmp;⼩贴⼠:MySQL8.0 中可以利⽤ ROW_NUMBER(),DENSE_RANK(),RANK() 三个窗⼝函数来实现排序需要注意的⼀点是 as 后的别名,千万不要与前⾯的函数名重名,否则会报错下⾯给出这三种函数实现排名的案例:–三条语句对于上⾯三种排名select xuehao,score, ROW_NUMBER() OVER(order by score desc) as row_r from scores_tb;select xuehao,score, DENSE_RANK() OVER(order by score desc) as dense_r from scores_tb;select xuehao,score, RANK() over(order by score desc) as r from scores_tb;– ⼀条语句也可以查询出不同排名SELECT xuehao,score,ROW_NUMBER() OVER w AS ‘row_r',DENSE_RANK() OVER w AS ‘dense_r',RANK() OVER w AS ‘r'FROM scores_tbWINDOW w AS (ORDER BY score desc);4.删除重复数据创建测试数据我们创建⼀个⼈员信息表并在⾥⾯插⼊⼀些重复的数据CREATE TABLE Person(id int auto_increment primary key comment ‘主键',Name VARCHAR(20) NULL,Age INT NULL,Address VARCHAR(20) NULL,Sex CHAR(2) NULL);INSERT INTO Person(ID,Name,Age,Address,Sex)VALUES( 1, ‘张三', 18, ‘北京路18号', ‘男' ),( 2, ‘李四', 19, ‘北京路29号', ‘男' ),( 3, ‘王五', 19, ‘南京路11号', ‘⼥' ),( 4, ‘张三', 18, ‘北京路18号', ‘男' ),( 5, ‘李四', 19, ‘北京路29号', ‘男' ),( 6, ‘张三', 18, ‘北京路18号', ‘男' ),( 7, ‘王五', 19, ‘南京路11号', ‘⼥' ),( 8, ‘马六', 18, ‘南京路19号', ‘⼥' );题⽬:数据库中存在重复记录,删除保留其中⼀条我们发现除了⾃增长ID不同以为,有⼏条其他字段都重复的数据出现第⼀步:找出重复的数据mysql>SELECT MAX(ID) ID,Name,Age,Address,SexFROM PersonGROUP BY Name,Age,Address,SexHAVING COUNT(1)>1⼩贴⼠:HAVING将分组后统计出来的数量⼤于1的数据⾏,就是我们要找的重复数据上⾯⽤Max函数或者Min函数均可,只是为了保证取出来的数据的唯⼀性。
sql去除重复的几种方法
sql去除重复的⼏种⽅法
所以⽤这样⼀句SQL就可以去掉重复项了:
select * from msg group by terminal_id;
SQL中distinct的⽤法(四种⽰例分析)
⽰例1
select distinct name from A
执⾏后结果如下:
⽰例2
select distinct name, id from A
执⾏后结果如下:
实际上是根据“name+id”来去重,distinct同时作⽤在了name和id上,这种⽅式Access和SQL Server同时⽀持。
⽰例3:统计
select count(distinct name) from A; --表中name去重后的数⽬, SQL Server⽀持,⽽Access不⽀持
select count(distinct name, id) from A; --SQL Server和Access都不⽀持
⽰例4
select id, distinct name from A; --会提⽰错误,因为distinct必须放在开头
其他
distinct语句中select显⽰的字段只能是distinct指定的字段,其他字段是不可能出现的。
例如,假如表A有“备注”列,如果想获取distinc name,以及对应的“备注”字段,想直接通过distinct是不可能实现的。
SQL中常见的三种去重方法
SQL中常见的三种去重⽅法在 MySQL 中通常是使⽤ distinct 或 group by⼦句,但在⽀持窗⼝函数的 sql(如Hive SQL、Oracle等等)中还可以使⽤ row_number 窗⼝函数进⾏去重。
举个栗⼦,现有这样⼀张表 task:备注:task_id: 任务id;order_id: 订单id;start_time: 开始时间注意:⼀个任务对应多条订单我们需要求出任务的总数量,因为 task_id 并⾮唯⼀的,所以需要去重:distinct-- 列出 task_id 的所有唯⼀值(去重后的记录)-- select distinct task_id-- from Task;-- 任务总数select count(distinct task_id) task_numfrom Task;distinct 通常效率较低。
它不适合⽤来展⽰去重后具体的值,⼀般与 count 配合⽤来计算条数。
distinct 使⽤中,放在 select 后边,对后⾯所有的字段的值统⼀进⾏去重。
⽐如distinct后⾯有两个字段,那么 1,1 和 1,2 这两条记录不是重复值。
group by-- 列出 task_id 的所有唯⼀值(去重后的记录,null也是值)-- select task_id-- from Task-- group by task_id;-- 任务总数select count(task_id) task_numfrom (select task_idfrom Taskgroup by task_id) tmp;row_numberrow_number 是窗⼝函数,语法如下:row_number() over (partition by <⽤于分组的字段名> order by <⽤于组内排序的字段名>)其中 partition by 部分可省略。
-- 在⽀持窗⼝函数的 sql 中使⽤select count(case when rn=1then task_id else null end) task_numfrom (select task_id, row_number() over (partition by task_id order by start_time) rnfrom Task) tmp;。
sql查询去除重复值语句
sql查询去除重复值语句sql 单表/多表查询去除重复记录单表distinct多表group bygroup by 必须放在 order by 和 limit之前,不然会报错************************************************************************************1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最⼩的记录delete from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)3、查找表中多余的重复记录(多个字段)select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)4、删除表中多余的重复记录(多个字段),只留有rowid最⼩的记录delete from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)5、查找表中多余的重复记录(多个字段),不包含rowid最⼩的记录select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)(⼆)⽐⽅说在A表中存在⼀个字段“name”,⽽且不同记录之间的“name”值有可能会相同,现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;Select Name,Count(*) From A Group By Name Having Count(*) > 1如果还查性别也相同⼤则如下:Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1(三)⽅法⼀declare @max integer,@id integerdeclare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1open cur_rowsfetch cur_rows into @id,@maxwhile @@fetch_status=0beginselect @max = @max -1set rowcount @maxdelete from 表名 where 主字段 = @idfetch cur_rows into @id,@maxendclose cur_rowsset rowcount 0⽅法⼆"重复记录"有两个意义上的重复记录,⼀是完全重复的记录,也即所有字段均重复的记录,⼆是部分关键字段重复的记录,⽐如Name字段重复,⽽其他字段不⼀定重复或都重复可以忽略。
SQL语句去除重复项
SQL语句去除重复项SQL语句去除重复字段项直接上代码--测试数据库Create table Test(Id int primary key identity(1,1) not null,name varchar(100) null,age varchar(100) null)GO--测试数据insert into Test values('aa',18)insert into Test values('aa',19)insert into Test values('aa',20)insert into Test values('bb',25)insert into Test values('bb',20)insert into Test values('cc',22)--解析--分组SELECT name FROM TestGROUP BY name--出现次数SELECT name,COUNT(name) AS出现次数FROM TestGROUP BY name--有重复的数据SELECT NAME,COUNT(NAME) AS重复数量FROM TESTGROUP BY nameHAVING COUNT(name)>1--找出重复项中ID 最⼤的⼀个SELECT NAME,COUNT(NAME) AS重复数量,MAX(ID)AS最⼤的ID FROM TESTGROUP BY nameHAVING COUNT(name)>1--找出重复项中ID 最⼩的⼀个SELECT NAME,COUNT(NAME) AS重复数量,MIN(ID)AS最⼩的ID FROM TESTGROUP BY nameHAVING COUNT(name)>1--找出整个表中的重复数据;SELECT*FROM TESTWHERE name IN(SELECT name FROM TESTGROUP BY nameHAVING COUNT(name)>1)--删除所有重复出现过的项DELETE FROM TESTWHERE ID IN(SELECT ID FROM TEST WHERE name IN(SELECT name FROM TESTGROUP BY nameHAVING COUNT(name)>1))--删除重复项,保留重复项中的⼀个(通常⽤的是这个)--第⼀步:找出重复项--第⼆步:找出重复项中的最⼤id--第三步:删除重复项中ID NOT IN (第⼆步)DELETE FROM TESTWHERE ID IN(SELECT ID FROM TEST WHERE name IN(SELECT name FROM TEST GROUP BY name HAVING COUNT(name)>1) ANDID NOT IN(SELECT MAX(ID) FROM TEST GROUP BY name HAVING COUNT(name)>1))完美。
sql针对某一字段去重,并且保留其他字段
sql针对某⼀字段去重,并且保留其他字段今天客户提了⼀个⼩需求,希望我能提供⼀条sql语句,帮助他对数据中 _field 这个字段的值去重,并且保留其他字段的数据。
第⼀反应是select distinct,但这种语句在对某个字段去重时,⽆法保留其他字段,所以select distinct不成⽴。
因为⽤户对去重没有要求,字段值重复时保留任意⼀⾏就⾏,所以我想到当字段值重复时,选出对应主键最⼤的那条数据作为保留数据,这样可以实现⽤户的去重需求。
但是⽤户的表中⼜没有主键,没办法,我们只好先使⽤窗⼝函数创建主键了。
因为平时喜欢⽤hive on spark写sql,所以sql语句使⽤中间表的形式来写,_field为去重字段,other_fields为原表table中_field外的其他字段1.创建主键(存在主键则⽆需创建,窗⼝函数需要遍历所有⾏数据,数据量⼤时会很慢)TEMP table1 = select row_number() over (order by _field) as id, _field, other_fields from table2.选出每个_field对应的最⼤主键TEMP table2 = select max(id) as max_id from table1 group by _field3.找出选中的主键对应的原表数据TEMP table3 = select _field, other_fields from table2 left join table on table2.max_id = table1.idOUTPUT table3中间表写法看起来可能有些乱,对于mysql这种⽀持嵌套查询的数据库来说,写起来更好理解id为主键,_field为去重字段,other_fields为原表table中_field外的其他字段select * from table where id in (select max(id) from table group by _field);。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一个简单的去除重复字段的SQL查询语句2009-11-16 17:12一个简单的去除重复字段的SQL查询语句[2008-11-04 16:01:15 by rainoxu] | 分类:我的知识库今天公司里让.Net程序修改一个程序,需要去掉输出中的重复楼盘名称,一开始想到的是Distinct,但死路不通,只能改道,最终偶在网上找到了一个思路,修改了一下就有了。
先看所有记录(这是我在测试的数据库里做的):OK,我们这样来消除重复项:1.select * from table1 as awhere not exists(select 1 from table1 where logID=a.LogID and ID>a.ID)2.最近做一个数据库的数据导入功能,发现联合主键约束导致不能导入,原因是源表中有重复数据,但是源表中又没有主键,很是麻烦。
经过努力终于解决了,现在就来和大家分享一下,有更好的办法的可以相互交流。
有重复数据主要有一下几种情况:1.存在两条完全相同的纪录这是最简单的一种情况,用关键字distinct就可以去掉example:select distinct * from table(表名) where (条件)2.存在部分字段相同的纪录(有主键id即唯一键)如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组example:select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])3.没有唯一键ID这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:example:select identity(int1,1) as id,* into newtable(临时表) from table select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])drop table newtable关于一个去除重复记录的sql语句2009-8-24 16:33提问者:lichuanbao1234|悬赏分:30 |浏览次数:1075次我要查询一个表中content字段相同的记录的详细信息。
其中每条记录都有一个标识符state,0表示未发送,1表示已发送。
我要统计所有content相同的记录的信息,包括其中已发送(state=1)的记录。
请问大家看看我这样写有什么问题?select distinct content,name,push_date,total,e.total_sended fromtbl_jingwei_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and content=a.content) e这样查出的其他字段都是符合要求的,唯独e.total_sended的结果出问题,它显示的是表中所有state=1的记录,请问大家我要怎么改呢?问题补充:这个sql语句是不对的。
表a是错误的。
请大家指点迷津,我要统计content相同并且state为1的记录数目。
谢谢各位。
我就是想去掉重复记录并统计一下,只不过如果state=1的话,我要统计一下state=1的记录数。
前提是这些记录的content是相同的。
二楼回答的不对,这和我写的是一样的,a表是不能在e表中用的。
2009-8-24 16:57最佳答案select distinct content,name,push_date,total,sum(case state when 1 then 1 when 0 then 0 end) as total_sended from tbl_jingwei_push以上,希望对你有所帮助!select distinct content,name,push_date,total,e.total_sended fromtbl_jingwei_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and e.content=a.content) e|评论2009-8-24 16:54 hrhero|五级select distinct content,name,push_date,total,e.total_sended fromtbl_jingwei_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and content=a.content group by content ) e试试这样SQLServer:Distinct和Group by去除重复字段记录2010-10-14 11:31:27| 分类:默认分类| 标签:|字号大中小订阅重复记录有两个意义,一是完全重复的记录,也即所有字段均重复的记录二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。
1、对于第一种重复,比较容易解决,使用select distinct * from tableName就可以得到无重复记录的结果集。
如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。
2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as autoID into #Tmp2 from #Tmp group by Nameselect * from #Tmp where autoID in(select autoID from #tmp2)最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)其它的数据库可以使用序列,如:create sequence seq1;select seq1.nextval as autoID, * into #Tmp from tableName============zuolo: 我根据上面实例得到所需要的语句为SELECT MAX(id) AS ID,Prodou_id,FinalDye FROM anwell..tblDBDdata GROUP BY Prodou_id,FinalDye ORDER BY id,之前一直想用Distinct来得到指定字段不重复的记录是个误区。
如何写一个SQL语句,能检索出所有某个字段内容有重复的记录A :比如:Name Adress Tele1 Yao China 1102 Zhang Moon 1103 Wang China 1104 Wang China 1105 Yao China 110根据姓名字段检索以后,能筛选出的记录有:1,3,4,5select b.id from table as b,(select count(field),field from table where count(field)>2 group by field) as a where a.field=b.fieldSELECT id from tb where dcount("id","tb","name='"&name&"'")>1附:DCount FunctionSee Also SpecificsYou can use the DCount function to determine the number of records that are in a specified se t of records (a domain). Use the DCount function in Visual Basic, a macro, a query expression, or a calculated control.For example, you could use the DCount function in a module to return the number of records in an Orders table that correspond to orders placed on a particular date.DCount(expr, domain, [criteria])The DCount function has the following arguments.Argument Descriptionexpr An expression that identifies the field for which you want to count records. It can be a s tring expression identifying a field in a table or query, or it can be an expression that performs a calculation on data in that field. In expr, you can include the name of a field in a table,a control on a form, a constant, or a function. If expr includes a function, it can be either bu ilt-in or user-defined, but not another domain aggregate or SQL aggregate function.domain A string expression identifying the set of records that constitutes the domain. It can be a table name or a query name for a query that does not require a parameter.criteria An optional string expression used to restrict the range of data on which the DCount fu nction is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, the DCount function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise the DCount function returns a Null.RemarksUse the DCount function to count the number of records in a domain when you don't need toknow their particular values. Although the expr argument can perform a calculation on a field, t he DCount function simply tallies the number of records. The value of any calculation performed by expr is unavailable.Use the DCount function in a calculated control when you need to specify criteria to restrict th e range of data on which the function is performed. For example, to display the number of ord ers to be shipped to California, set the ControlSource property of a text box to the following e xpression:=DCount("[OrderID]", "Orders", "[ShipRegion] = 'CA'")If you simply want to count all records in domain without specifying any restrictions, use the C ount function.Tip The Count function has been optimized to speed counting of records in queries. Use the Count function in a query expression instead of the DCount function, and set optional criteria toenforce any restrictions on the results. Use the DCount function when you must count records in a domain from within a code module or macro, or in a calculated control.You can use the DCount function to count the number of records containing a particular field that isn't in the record source on which your form or report is based. For example, you could display the number of orders in the Orders table in a calculated control on a form based on t he Products table.The DCount function doesn't count records that contain Null values in the field referenced by ex pr unless expr is the asterisk (*) wildcard character. If you use an asterisk, the DCount function calculates the total number of records, including those that contain Null fields. The following e xample calculates the number of records in an Orders table.intX = DCount("*", "Orders")If domain is a table with a primary key, you can also count the total number of records by s etting expr to the primary key field, since there will never be a Null in the primary key field.If expr identifies multiple fields, separate the field names with a concatenation operator, either an ampersand (&) or the addition operator (+). If you use an ampersand to separate the fields, theDCount function returns the number of records containing data in any of the listed fields. If yo u use the addition operator, the DCount function returns only the number of records containing data in all of the listed fields. The following example demonstrates the effects of each operator when used with a field that contains data in all records (ShipName) and a field that contains n o data (ShipRegion).intW = DCount("[ShipName]", "Orders")intX = DCount("[ShipRegion]", "Orders")intY = DCount("[ShipName] + [ShipRegion]", "Orders")intZ = DCount("[ShipName] & [ShipRegion]", "Orders")Note The ampersand is the preferred operator for performing string concatenation. You should a void using the addition operator for anything other than numeric addition, unless you specifically wish to propagate Nulls through an expression.Unsaved changes to records in domain aren't included when you use this function. If you want the DCount function to be based on the changed values, you must first save the changes by cli cking Save Record on the Records menu, moving the focus to another record, or by using the Update method.ExampleThe following function returns the number of orders shipped to a specified country after a specif ied ship date. The domain is an Orders table.Public Function OrdersCount(ByVal strCountry As String, _ByVal dteShipDate As Date) As IntegerOrdersCount = DCount("[ShippedDate]", "Orders", _"[ShipCountry] = '" & strCountry & _"' AND [ShippedDate] > #" & dteShipDate & "#")End FunctionTo call the function, use the following line of code in the Immediate window::OrdersCount "UK", #1/1/96#我现在正在DAO里面,头都大了就用简单的吧CString strSQL;帮我把上面那一句怎么放入CString 里面?谢谢啊,已经搞定,留给后来人吧:)strSQL.Format("SELECT * from WeakTab where dcount(%c*%c,%cWeakTab%c,%cname='%c&name&% c'%c)>1", 34,34,34,34,34,34,34,34);比较麻烦的sql语句,分页的同时要去除某个字段的重复值作者: sql语句来源: sql数据库学习网时间: 2011-05-24 阅读: 次在线投稿回应楼主:或你划定只保留熬头个siteid,后面这个字段反复的记载不要。