我正在尝试在出现重复键时更新一行中的列。如果列当前不等于“C”,我只想更新列。我已经查看了所有在线信息,这看起来还不错。但是,我得到的错误消息是语法附近有错误:
WHERE VALUES(cad_status_flag) <> 'C' OR VALES(cad_status_flag) IS NULL
我试过没有VALUES,即WHERE cad_status_flag&lt;&gt; '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不允许使用WHERE,因此我将其替换为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());
}
现在我在addYMAwards5中收到错误“SQLException:com.mysql.jdbc.exception.jdbc.exzception.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册在'第1行'附近使用正确的语法
答案 0 :(得分:0)
我发现很难遵循你的逻辑。您似乎希望在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);
答案 1 :(得分:0)
MySQL不允许WHERE,你必须使用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);");
注意:VALUES(cad_status_flag)可以替换为?只要你添加ps5.setString(5,groupCompleted);在准备好的声明中。