MSSQL取得或删除重复数据

1.取得不重复的数据

select * from Persons where Id in
(
SELECT    MAX(Id) AS Expr1
FROM    Persons
GROUP BY Name, Gender
)

2.删除重复的数据【MAX换成MIN会有不同的效果】

delete from Persons
where Id not in
(
SELECT    MAX(Id) AS Expr1
FROM    Persons
GROUP BY Name, Gender
)
时间: 2024-10-09 21:24:09

MSSQL取得或删除重复数据的相关文章

MSSQL如何在没有主键的表中删除重复数据

为了对重复数据进行实验,下面建一个设计不太好(没有主键)表并插入了一些重复数据: create database testdb use testdb ; go create table DupsNoPK (Col1 int Null, Col2 char(5) Null ); go insert DupsNoPK(Col1,Col2) Values(1,'abc'), (2,'abc'), (2,'abc'), (2,'abc'), (7,'xyz'), (7,'xyz'); 为了验证表确实有重

mysql如何删除重复数据并保留ID最大的数据

题:有一张test表,里面含有三个字段id,name,sex. 问:如何查询重复数据? select name,sex,count(*) from test group by name,sex HAVING count(*)>1. 问:如何删除重复数据并保留ID最大的数据? delete a from test a join (select name,sex,count(*) ,max(id) as id from test group by name,sex HAVING count(*)>

SQL语句删除重复数据

1.如表中没有主键,先添加自动增长主键 alter table 表名 add 列名 int identity (1,1) primary key 2.删除重复数据 delete from 表名 where id not in (select min(id) from 表名 group by id)

mysql 删除重复数据

如题:mysql 数据库删除重复数据 因为是mysql 所以其他数据哭的命令在mysql 中是不能使用的.不要想当然的使用sql 脚本 delete from table1 where field1 in (select field1 from table1 group by field1 having count(field1) > 1) and rowid not in (select min(rowid) from table1 group by field1 having count(f

mysql 删除重复数据的sql语句

CREATE TABLE tmp AS SELECT id FROM get_review_url WHERE (no,title,name,content) IN (SELECT no,title,name,content FROM get_review_url GROUP BY no,title,name,content HAVING COUNT(*) > 1) AND id NOT IN (SELECT MIN(id) FROM get_review_url GROUP BY no,tit

取两个DataTable的交集,删除重复数据

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /// <summary>         /// 取两个DataTable的交集,删除重复数据         /// </summary>         /// <param name="sourceDataTable">源DataTable</param>         

MYSQL删除重复数据

delete from co_jobinformation cwhere c.name in (select cc.name from co_jobinformation cc group by  cc.name   having count(cc.name) > 1)and rowid not in (select min(rowid) from co_jobinformation e group by e.name having count(e.name )>1) 之前在oracle数据库

Mysql开发技巧之删除重复数据

Mysql利用联表查询和分组来删除重复数据 //删除表中重复的id,保留最大的id mysql> select * from user; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | | 4 | d | | 5 | a | | 6 | a | | 7 | c | | 8 | d | +----+------+ 8 rows in set (0.08 sec) mysql> delete a fro

Oracle 删除重复数据

DELETE FROM tb E WHERE E.ROWID > (SELECT MIN(X.ROWID)                    FROM tb X                   WHERE X.APR_BSS_ID = E.APR_BSS_ID                   AND X.ID=E.ID                   AND X.NAME=E.NAME                 ); Oracle 删除重复数据