一个表中有重复记录如何用SQL语句查询出来。。。?
答案:4 悬赏:0 手机版
解决时间 2021-01-21 07:16
- 提问者网友:末路
- 2021-01-20 09:17
一个表中有重复记录如何用SQL语句查询出来。。。?
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-01-20 09:49
select * from tablename where 重复字段1 in (select 重复字段1 from tablename group by 重复字段1,重复字段2 having count(*)>1)。
SQL重复记录查询方法:1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count (peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where 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 a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
SQL重复记录查询方法:1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count (peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where 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 a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
全部回答
- 1楼网友:持酒劝斜阳
- 2021-01-20 13:04
Select Row_Number() Over(Partition By 排序字段 Order By 不同的字段 Desc) As Num,
t.*
from table
where Num = 2
t.*
from table
where Num = 2
- 2楼网友:千夜
- 2021-01-20 11:56
不知道你什么数据库.
如果数据库支持 ROW_NUMBER() 函数的话, 倒是很省事的.
-- 首先创建测试表
CREATE TABLE test_delete(
name varchar(10),
value INT
);
go
-- 测试数据,其中 张三100 与 王五80 是完全一样的
INSERT INTO test_delete
SELECT '张三', 100
UNIOn ALL SELECT '张三', 100
UNIOn ALL SELECT '李四', 80
UNIOn ALL SELECT '王五', 80
UNIOn ALL SELECT '王五', 80
UNIOn ALL SELECT '赵六', 90
UNIOn ALL SELECT '赵六', 70
go
-- 首先查询一下, ROW_NUMBER 效果是否满足预期
SELECT
ROW_NUMBER() OVER (PARTITION BY name, value ORDER BY (SELECT 1) ) AS no,
name,
value
FROM
test_delete
no name value
----- ---------- -----------
1 李四 80
1 王五 80
2 王五 80
1 张三 100
2 张三 100
1 赵六 70
1 赵六 90
从结果上可以看到,如果有重复的,完全一样的话, no 是有大于1的。
如果数据库支持 ROW_NUMBER() 函数的话, 倒是很省事的.
-- 首先创建测试表
CREATE TABLE test_delete(
name varchar(10),
value INT
);
go
-- 测试数据,其中 张三100 与 王五80 是完全一样的
INSERT INTO test_delete
SELECT '张三', 100
UNIOn ALL SELECT '张三', 100
UNIOn ALL SELECT '李四', 80
UNIOn ALL SELECT '王五', 80
UNIOn ALL SELECT '王五', 80
UNIOn ALL SELECT '赵六', 90
UNIOn ALL SELECT '赵六', 70
go
-- 首先查询一下, ROW_NUMBER 效果是否满足预期
SELECT
ROW_NUMBER() OVER (PARTITION BY name, value ORDER BY (SELECT 1) ) AS no,
name,
value
FROM
test_delete
no name value
----- ---------- -----------
1 李四 80
1 王五 80
2 王五 80
1 张三 100
2 张三 100
1 赵六 70
1 赵六 90
从结果上可以看到,如果有重复的,完全一样的话, no 是有大于1的。
- 3楼网友:冷風如刀
- 2021-01-20 11:20
select * from tablename where 重复字段1 in (select 重复字段1 from tablename group by 重复字段1,重复字段2 having count(*)>1)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯