SQL Query删除重复的记录不起作用

时间:2013-09-11 16:05:53

标签: java mysql sql

美好的一天。我的Java代码中有一个查询删除表中的重复行。最初它工作了一段时间我没有触及该项目。但是几天前运行该文件时,我的代码抛出异常。这是我的代码:

String query = "DELETE error_log FROM error_log INNER JOIN "
                + "(SELECT  min(id) minid, service_source, channel,transaction_type, provider_name,     pido_account, beneficiary_id, error_description, error_date FROM error_log "
                + "GROUP BY service_source, channel, transaction_type, provider_name, pido_account, beneficiary_id, error_description, error_date "
                + "HAVING COUNT(1) > 1 AS duplicates ON "
                        + "(duplicates.service_source = error_log.service_source AND duplicates.channel = error_log.channel "
                    + "AND duplicates.transaction_type = error_log.transaction_type AND duplicates.provider_name = error_log.provider_name "
                    + "AND duplicates.pido_account = error_log.pido_account AND duplicates.beneficiary_id = error_log.beneficiary_id "
                    + "AND duplicates.error_description = error_log.error_description AND duplicates.error_date = error_log.error_date "
                    + "AND duplicates.minid <> error_log.id"
                        + ")"
                    + ")";

        int deploy = duplicate.executeUpdate(query);

之后我得到了这个:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'AS duplicates ON附近使用正确的语法(第1行的duplicates.service_source = error_log.service_source AND dupli'。

如何更正此问题并从表格中删除重复项?

1 个答案:

答案 0 :(得分:0)

您在)行上缺少+ "HAVING COUNT(1) > 1 AS duplicates ON ",并且最后还有)

String query = "DELETE error_log FROM error_log INNER JOIN "
                + "(SELECT  min(id) minid, service_source, channel,transaction_type, provider_name,     pido_account, beneficiary_id, error_description, error_date FROM error_log "
                + "GROUP BY service_source, channel, transaction_type, provider_name, pido_account, beneficiary_id, error_description, error_date "
                + "HAVING COUNT(1) > 1 ) AS duplicates ON "
                        + "(duplicates.service_source = error_log.service_source AND duplicates.channel = error_log.channel "
                    + "AND duplicates.transaction_type = error_log.transaction_type AND duplicates.provider_name = error_log.provider_name "
                    + "AND duplicates.pido_account = error_log.pido_account AND duplicates.beneficiary_id = error_log.beneficiary_id "
                    + "AND duplicates.error_description = error_log.error_description AND duplicates.error_date = error_log.error_date "
                    + "AND duplicates.minid <> error_log.id"
                    + ")";

        int deploy = duplicate.executeUpdate(query);

如果您没有进行任何更改并且它已停止工作,1)您确定已测试此代码吗? 2)有没有其他人在你不知情的情况下做出任何改变?