INNER JOIN的UPDATE失败但相关的SELECT语句有效

时间:2012-08-15 11:45:25

标签: mysql sql-update inner-join

以下UPDATE语句是否会产生错误

UPDATE `t1`
INNER JOIN `t2` ON `t2`.`id`=`t1`.`t2_id`
INNER JOIN `t3` ON `t2`.`t3_id` = `t3`.`id` AND `t3`.`a_id` = '123'
WHERE `t2`.`date` > '2012-08-14'
    AND `t2`.`status` = 'pending'
SET `t1`.`active` = '0';

我得到的错误是:

#1064 - 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 'WHERE `t2`.`date` > '2012-08-14' AND `t2`.`statu' at line 4

以下(相关)SELECT语句可以正常工作

SELECT `t1`.*
FROM `t1`
INNER JOIN `t2` ON `t2`.`id`=`t1`.`t2_id`
INNER JOIN `carer` ON `t2`.`t3_id` = `t3`.`id` AND `t3`.`a_id` = '123'
WHERE `t2`.`date` > '2012-08-14'
    AND `t2`.`status` = 'pending'

2 个答案:

答案 0 :(得分:7)

MySQL UPDATE syntax是:

UPDATE [LOW_PRIORITY] [IGNORE] table_references  
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...  
    [WHERE where_condition]  

使用此:

UPDATE `t1`
INNER JOIN `t2` ON `t2`.`id`=`t1`.`t2_id`
INNER JOIN `t3` ON `t2`.`t3_id` = `t3`.`id` AND `t3`.`a_id` = '123'

SET `t1`.`active` = '0'

WHERE `t2`.`date` > '2012-08-14'
    AND `t2`.`status` = 'pending' ;

答案 1 :(得分:0)

使用此语法执行此操作:

UPDATE r
INNER JOIN ENGING_DERIVE r
FROM ENGING_DERIVE
SET r.TUNING_PERFORM = r.BORE * r.STROKE
WHERE t.ID = t.ID;