我有一个复杂的更新,我想在重复键更新时更改为插入。插入忽略工作,更新工作;但是当我结合时,我得到了错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN at_award_description ON at_cub_award_date.ad_id = at_award_descriptio' at line 1
原始的INSERT是:
String insertQry = ("INSERT IGNORE INTO at_cub_award_date (ca_id, ad_id, cad_status_flag) " +
"VALUES (?, (SELECT ad_id " +
"FROM at_award_description " +
"WHERE at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_order = '1'), ?);");
原始更新是:
String updateQry = ("UPDATE at_cub_award_date dest " +
"INNER JOIN at_award_description " +
"ON dest.ad_id = at_award_description.ad_id " +
"AND at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_order = '1' " +
"SET dest.cad_status_flag = 'S' " +
"WHERE dest.ca_id = ? " +
//No Group Completion date
"AND (SELECT noCompletedGroup FROM " +
"(SELECT COUNT(*) noCompletedGroup " +
"FROM at_cub_award_date, at_award_description " +
"WHERE at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_detail LIKE '%Completed:%' " +
"AND at_award_description.ad_id = at_cub_award_date.ad_id " +
"AND at_cub_award_date.ca_id = ? " +
"AND at_cub_award_date.cad_task_completion_date IS NOT NULL) AS T1) = 0 " +
//A task in the Group has completed
"AND (SELECT noCompletedTasks FROM " +
"(SELECT COUNT(*) noCompletedTasks " +
"FROM at_cub_award_date, at_award_description " +
"WHERE at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_id = at_cub_award_date.ad_id " +
"AND at_cub_award_date.ca_id = ? " +
"AND at_cub_award_date.cad_task_completion_date IS NOT NULL) AS T2) > 0;");
重复密钥更新的新INSERT是:
String insertQry = ("INSERT INTO at_cub_award_date (ca_id, ad_id, cad_status_flag) " +
"VALUES (?, (SELECT ad_id " +
"FROM at_award_description " +
"WHERE at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_order = '1'), ?) " +
"ON DUPLICATE KEY UPDATE at_cub_award_date " +
"INNER JOIN at_award_description " +
"ON at_cub_award_date.ad_id = at_award_description.ad_id " +
"AND at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_order = '1' " +
"SET at_cub_award_date.cad_status_flag = 'S' " +
"WHERE at_cub_award_date.ca_id = ? " +
//No Group Completion date
"AND (SELECT noCompletedGroup FROM " +
"(SELECT COUNT(*) noCompletedGroup " +
"FROM at_cub_award_date, at_award_description " +
"WHERE at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_detail LIKE '%Completed:%' " +
"AND at_award_description.ad_id = at_cub_award_date.ad_id " +
"AND at_cub_award_date.ca_id = ? " +
"AND at_cub_award_date.cad_task_completion_date IS NOT NULL) AS T1) = 0 " +
//A task in the Group has completed
"AND (SELECT noCompletedTasks FROM " +
"(SELECT COUNT(*) noCompletedTasks " +
"FROM at_cub_award_date, at_award_description " +
"WHERE at_award_description.aw_id = ? " +
"AND at_award_description.ad_group = ? " +
"AND at_award_description.ad_id = at_cub_award_date.ad_id " +
"AND at_cub_award_date.ca_id = ? " +
"AND at_cub_award_date.cad_task_completion_date IS NOT NULL) AS T2) > 0;");
将两者分开是否有任何问题?我读到第二个陈述可能在第一个陈述之前开始并产生虚假结果。
有更好的方法吗?