当出现重复键时,我正在尝试更新行中的列。我只希望在列当前不等于"C“的情况下更新该列。我已经审查了所有的在线信息,这看起来还可以。但是,我得到的错误消息是,在语法上出现了一个错误:
WHERE VALUES(cad_status_flag) <> 'C' OR VALES(cad_status_flag) IS NULL我尝试过没有值,即cad_status_flag <> 'C‘或ad_status_flag为NULL,并出现相同的错误消息。
SQL是:
String selectQry5 = ("INSERT INTO at_cub_award_date " +
"(ca_id, ad_id, cad_task_completion_date, cad_status_flag) " +
"VALUES (?, ?, ?, ?) " +
//"ON DUPLICATE KEY UPDATE cad_status_flag=? WHERE VALUES(cad_status_flag) <> 'C' OR VALUES(cad_status_flag) IS NULL;");MySQL不允许任何位置,所以我用IF替换了它
"ON DUPLICATE KEY UPDATE " +
"cad_status_flag = IF(cad_status_flag <> 'C' OR cad_status_flag IS NULL, VALUES(cad_status_flag), cad_status_flag;");
//Create the at_cub_award_date order 1 row
try {
// Create a statement and execute the query on it
ps5.setString(1, caid);
ps5.setString(2, adid);
ps5.setString(3, awardedDate);
ps5.setString(4, groupCompleted);
ps5.setString(5, groupCompleted);
ps5.executeUpdate();
//Get foreign key from insert into at_cub_awards
ResultSet rs = ps5.getGeneratedKeys();
if (rs.next()) {
ymAwards = new YMAwards(rs.getString(1), null, null, null, null);
};
// Clean up
ps5.close();
c.close();
System.out.println("addYMAwards5 completed: ");
} catch (SQLException se) {
System.out.println("SQLException in addYMAwards5: " + se.toString());
} catch (Exception e) {
System.out.println("Errors occurred in addYMAwards5: " + e.toString());
}现在我得到了错误“com.mysql.jdbc.exception.jdbc.exzception.jdbc4.MySQLSyntaxErrorException:中的SQLException:您在addYMAwards5语法中有一个错误;检查与MySQL服务器版本相对应的手册,以便在第1行使用正确的语法
发布于 2016-02-24 01:34:22
我觉得很难理解你的逻辑。您似乎希望在CASE子句中包含一个ON DUPLICATE KEY UPDATE语句:
INSERT INTO at_cub_award_date(ca_id, ad_id, cad_task_completion_date, cad_status_flag)
SELECT ?, ?, ?, ?
FROM dual
ON DUPLICATE KEY UPDATE
cad_status_flag = (CASE WHEN VALUES(cad_status_flag) <> 'C' OR VALUES(cad_status_flag) IS NULL
THEN ? ELSE cad_status_flag
END);发布于 2016-02-24 04:08:49
MySQL不允许在哪里,您必须使用IF。
String selectQry5 = ("INSERT INTO at_cub_award_date " +
"(ca_id, ad_id, cad_task_completion_date, cad_status_flag) " +
"VALUES (?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE " +
"cad_status_flag = IF(cad_status_flag <> 'C' OR cad_status_flag IS NULL, VALUES(cad_status_flag), cad_status_flag);");注意:值(Cad_status_flag)可以替换为?只要您在准备好的语句中添加ps5.setString(5,groupCompleted)。
https://stackoverflow.com/questions/35591609
复制相似问题