先查詢出知足某種條件的數據的最小ID,而後刪除最小ID之外的數據就實現了去重sql
select * from oms_relation_model orm where orm.fd_id= ( select min(t.fd_id) from oms_relation_model t where orm.fd_ekp_id=t.fd_ekp_id and orm.fd_ekp_id=t.fd_ekp_id ) ;
delete from oms_relation_model s where s.fd_id not in ( select orm.fd_id from oms_relation_model orm where orm.fd_id= ( select min(t.fd_id) from oms_relation_model t where orm.fd_ekp_id=t.fd_ekp_id and orm.fd_ekp_id=t.fd_ekp_id ) );
/** * 不重複的id列表 * * @return */ private List<String> getUniqueIds() { Session session = getBaseDao().getHibernateSession(); String sql = "select fdId from OmsRelationModel orm" + " where orm.fdId in" + " (select min(fdId) from OmsRelationModel t where orm.fdEkpId=t.fdEkpId and orm.fdAppPkId=t.fdAppPkId)"; Query query = session.createQuery(sql); List result = query.list(); logger.info("查詢重複數據,list=>" + JSONUtils.valueToString(result)); return result; } /** * 刪除重複的 * * @param idList */ private void delSame(List idList) { Session session = getBaseDao().getHibernateSession(); String sql = "delete from OmsRelationModel where fdId not in (:ids)"; Query query = session.createQuery(sql); query.setParameterList("ids", idList); int result = query.executeUpdate(); logger.info("查詢重複數據,list=>" + JSONUtils.valueToString(result)); session.flush(); session.clear(); }