更新最新记录

时间:2015-08-18 06:51:47

标签: mysql

我有一个以下的mysql查询来更新我的tblcem_appraisal_log

UPDATE tblcem_appraisal_log AS tblog
INNER JOIN (
            SELECT ta.id
            FROM tblcem_appraisal_log AS ta
            INNER JOIN tblcem_master_appraisal AS tm ON tm.id = ta.master_app_id        
            ORDER BY ta.id DESC
LIMIT 1) AS source ON source.id = tblog.id
SET tblog.quarter1 = tblcem_master_appraisal.quarter1 WHERE tblog.master_app_id=8;

虽然两个表中都存在列unknown column tblcem_master_appraisal.quarter1,但我收到错误quarter1。 确切需要放入SET来更新tblcem_master_appraisal表中的值。

2 个答案:

答案 0 :(得分:1)

您必须在子选择中选择列并使用该表的别名:

  UPDATE tblcem_appraisal_log AS tblog
     INNER JOIN (
                SELECT ta.id, tm.quarter1 
                FROM tblcem_appraisal_log AS ta
                INNER JOIN tblcem_master_appraisal AS tm ON tm.id =    ta.master_app_id        
                ORDER BY ta.id DESC
    LIMIT 1) AS source ON source.id = tblog.id
    SET tblog.quarter1 = source.quarter1 WHERE tblog.master_app_id=8;

答案 1 :(得分:0)

这适合我。

UPDATE tblcem_appraisal_log AS tblog
 INNER JOIN (
            SELECT ta.id, tm.quarter1 
            FROM tblcem_appraisal_log AS ta
            INNER JOIN tblcem_master_appraisal AS tm ON tm.id = ta.master_app_id 
            WHERE ta.master_app_id=8    
            ORDER BY ta.id DESC LIMIT 1) AS source ON source.id = tblog.id
SET tblog.quarter1 = source.quarter1;

where条目放在join