删除重复数据sql语句

删除重复数据sql语句
方法一

假设有重复的字段为name,address,要求得到这两个字段唯一的结果集
select identity(int,1,1) as autoid, * into #tmp from tablename
select min(autoid) as autoid into #tmp2 from #tmp group by name,autoid
select * from #tmp where autoid in(select autoid from #tmp2)

方法二
declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from 表名 where 主字段 = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

方法三

如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select distinct * into #tmp from tablename
drop table tablename
select * into tablename from #tmp
drop table #tmp

实例

vieworder  id号      打卡机     消费类型    时间        价钱
1           a1       1号机     早餐       2006-08-03 07:09:23.000         2
2           a1       1号机     早餐       2006-08-03 07:10:13.000         2
3           a1       1号机     早餐       2006-08-03 07:10:19.000         2
4           a1       1号机     午餐       2006-08-03 12:02:10.000         5
5           a2       1号机     午餐       2006-08-03 12:11:10.000         5
6           a2       1号机     午餐       2006-08-03 12:12:10.000         5

代码

delete from 表 a
where exists(select * from 表 where  消费类型=a.消费类型 and 时间>=dateadd(minute,-2,a.时间) and 时间<a.时间)

删除之前先用select语句查看要被删除的数据
select *
from 表 a
where exists(select * from 表 where  消费类型=a.消费类型 and 时间>=dateadd(minute,-2,a.时间) and 时间<a.时间)

时间: 2024-09-15 19:32:32

删除重复数据sql语句的相关文章

PostgreSQL删除重复数据sql语句

PostgreSQL 库如何去除单表重复数据呢?可以通过 ctid 进行,下面是实验过程. 一.创建测试表  代码如下 复制代码 david=# create table emp ( david(# id int, david(# name varchar); CREATE TABLE david=# 二.插入测试数据  代码如下 复制代码 david=# insert into emp values (1, 'david'); INSERT 0 1 david=# insert into em

删除重复记录 sql语句

删除重复记录 sql语句 本教程为你提供了二款关于删除重复记录的sql语句代码.一个利用where in查询重复记录再实现删除,一个是用inner join来实例删除重记录. */ //方法一 delete from tablea where title in (select a.title from tablea a join tableb b on a.title = b.title) //方法二 delete a.* from tablea a inner join tableb b on

mysql 查询筛选重复数据sql语句

查询重复数据数量  代码如下 复制代码 select device_id from device group by device_id having count(device_id) > 1; 查询所有重复数据  代码如下 复制代码 select userid, device_id, create_date from device where device_id in (select device_id from device group by device_id having count(de

mysql中删除重复记录sql语句

删除重复记录方法一: 1. 新建一个临时表  代码如下 复制代码 create table tmp as select * from youtable group by  name(name为不希望有重复的列) 2. 删除原来的表  代码如下 复制代码 drop table youtable 3. 重命名表  代码如下 复制代码 alter table tmp rename youtable 但是这个方法有个问题,由临时表转变过来的最终表,其表结构会和原来的不一致,需要手工更改.这个问题,待解决

SQL删除重复数据方法

原文:SQL删除重复数据方法 例如: id           name         value 1               a                 pp 2               a                 pp 3               b                 iii 4               b                 pp 5               b                 pp 6            

sql删除重复数据的详细方法_MsSql

一. 删除完全重复的记录 完全重复的数据,通常是由于没有设置主键/唯一键约束导致的.测试数据: 复制代码 代码如下: if OBJECT_ID('duplicate_all') is not nulldrop table duplicate_all GO create table duplicate_all ( c1 int, c2 int, c3 varchar(100) ) GO insert into duplicate_all select 1,100,'aaa' union allse

mysql 数据库超强删除重复数据语句

月小升今天遇到的问题是students这个表有md这个字段重复.看看如何处理吧.  代码如下 复制代码 select * from students where md in (select md from students group by md having count(md) > 1) order by md 注明,这个被group的字段,请索引,否则很慢  代码如下 复制代码 delete from students where md in (select md from students

mysql教程删除数据sql语句用法

  mysql教程删除数据sql语句用法 DELETE FROM 语句用于从数据库表中删除记录. 语法 DELETE FROM table_name WHERE column_name = some_value 注释:SQL 对大小写不敏感.DELETE FROM 与 delete from 等效. 为了让 PHP 执行上面的语句,我们必须使用 mysql_query( 函数.该函数用于向 SQL 连接发送查询和命令. 例子 稍早时,我们在本教程中创建了一个名为 "Person" 的表

thinkphp中多表查询中防止数据重复的sql语句(必看)_php技巧

下面先来看看例子: table id name 1 a 2 b 3 c 4 c 5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多. select *, count(distinct name) from table group by name 结果: id name count(distinct name) 1 a 1 2 b 1 3 c 1 最后一 项是多余的,不用管就行了 tp2.0手册   搜索连贯操作 可看到相关的资料 SELECT cat_id, COUNT(*) AS