kb_nid是唯一键和c_nodes表;包括kb_nid,runState,startedTime字段

我想要在表更新之前,比较旧表,但会收到此错误:


  “ SQL错误(1054):“字段列表”中的未知列“ cn.runState””


INSERT INTO c_nodes(kb_nid,runState,startedTime)
    SELECT n.id, n.nState>10 as runState, NULL as startedTime
    FROM node n
    LEFT JOIN  c_nodes cn ON n.id =cn.kb_nid
    GROUP BY n.id
    ON DUPLICATE KEY UPDATE startedTime=IF(cn.runState<>VALUES(runState) ,NOW(),cn.startedTime)


我看不到任何问题。

编辑http://sqlfiddle.com/#!9/8f8a84

感谢帮助。

最佳答案

我用子查询修复了它。我猜错误原因是GROUP BY

INSERT INTO c_nodes(kb_nid,runState,startedTime)
SELECT n.id,runState,startedTime  FROM (
SELECT n.id, n.nState>10 as runState, NULL as startedTime
FROM node n
GROUP BY n.id) tt
LEFT JOIN  c_nodes cn ON tt.id =cn.kb_nid
ON DUPLICATE KEY UPDATE startedTime=IF(cn.runState<>VALUES(runState) ,NOW(),cn.startedTime)


参考:https://stackoverflow.com/a/14533265/3952108

关于mysql - mysql-在重复键更新时,加入表字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35533389/

10-11 03:14