新的mysql,查询抛出错误的语法

时间:2011-07-31 16:21:29

标签: java mysql

我试图从我的Java应用程序传递此查询,但它说我在UPDATE行中有错误,但我找不到有什么问题?你能看到我做错了吗?

int rs = st.executeUpdate("INSERT INTO timesheet (employeeID, statusCode, periodEndingDate, departmentCode, minutesMon,"+
            "minutesTue, minutesWed, minutesThu, minutesFri, minutesSat, minutesSun)"+
            " VALUES ('"+empID+"','"+statusCode+"','"+periodEndingDate+"','"+departmentCode+"','"+minutesMon+"','"+
             minutesTue+"','"+minutesWed+"','"+minutesThu+"','"+minutesFri+"','"+minutesSat+"','"+minutesSun+"')"+
             " ON DUPLICATE KEY UPDATE timesheet SET statusCode='"+statusCode+"', periodEndingDate='"+periodEndingDate+"', departmentCode='"+departmentCode+"',"+
            " minutesMon = '"+minutesMon+"', minutesTue='"+minutesTue+"', minutesWed='"+minutesWed+"', minutesThu='"+minutesThu+"', minutesFri='"+minutesFri+"',"+
            " minutesSat='"+minutesSat+"', minutesSun='"+minutesSun+"' WHERE periodEndingDate='"+periodEndingDate+"' AND employeeID='"+empID+"';");

1 个答案:

答案 0 :(得分:2)

您不能在WHERE中使用ON DUPLICATE KEY UPDATE子句。它正在更新副本。 如果UNIQUE上有(periodEndingDate, employeeID)索引,则它已经在没有where子句的情况下更新了正确的行。此外,丢失表名和Update子句中的SET

MySQL Docs on ON DUPLICATE KEY UPDATE