本文主要是介绍Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在调试接口遇到如上错误:
更新或者删除字段的时候,只想更新一条,结果有两条
这个是由于用户hibernate使用HQL更新和删除实体,一定需要先获取实体的ID,如下本意直接删除字段为某个值,但是会先查出对应的id,然后根据id去删除
但是由于ID有重复,导致报错(通常在开发环境,数据错乱,id 不应该重复)
解决方法:
查询及删除重复记录的SQL语句
1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断
select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录
DELETE from 表 WHERE (id) IN ( SELECT id FROM 表 GROUP BY id HAVING COUNT(id) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);
3、查找表中多余的重复记录(多个字段)
select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count() > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count()>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count() > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count()>1)
这篇关于Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!