本文目录一览:
请问在mysql中如何删除 两个字段相同的数据?
delete
from
dede_archives
where
[标题]=[标题]
and
[作者]=[作者]这个应该是不行的。我是要把重复的删除。但是要保留一条。比如有两篇文章。他们的作者
和标题相同的。那么这一般是重复的文章。所以我想把一篇删除。保留一篇。因为数据比较多。所以要用sql语句执行。大家帮忙想想怎么弄。是dedecms
里面主键盘应该只是id号码。大家帮忙一下啊。
mysql 根据两个字段值查询时如何去除重复数据
假设表名为test:
select * from (select *, concat(name,code) as __f from test order by date desc) __t group by __f;
mysql如何去除两个字段数据相同的记录
MySQL查询重复字段,及删除重复记录的方法
数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较。如果仅仅是查找数据库中name不重复的字段,很容易:
SELECT min(`id`),`name` FROM `table` GROUP BY `name`;
但是这样并不能得到说有重复字段的id值。(只得到了最小的一个id值)查询哪些字段是重复的也容易:
SELECT `name`,count(`name`) as count FROM `table` GROUP BY `name` HAVING count(`name`) 1 ORDER BY count DESC;
但是要一次查询到重复字段的id值,就必须使用子查询了,于是使用下面的语句。
SELECT `id`,`name` FROM `table` WHERE `name` in (
SELECT `name`
FROM `table`
GROUP BY `name` HAVING count(`name`) 1);
但是这条语句在mysql中效率太差,感觉mysql并没有为子查询生成零时表。于是使用先建立零时表:
create table `tmptable` as (
SELECT `name`
FROM `table`
GROUP BY `name` HAVING count(`name`) 1);
然后使用多表连接查询:
SELECT a.`id`, a.`name` FROM `table` a, `tmptable` t WHERE a.`name` = t.`name`;
结果这次结果很快就出来了。
========================
查询及删除重复记录的方法
(一)
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 count(*) 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (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 a
where (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)