insert …select … count … on duplicate key update error

喜你入骨 提交于 2019-12-23 03:04:46

问题


I created a table to hold counts of work orders by asset tag. I have 2 fields, asset_tag (which is unique) and the wo_count. I am trying to write a query that will insert/update the counts in the table. Through research on ON DUPLICATE KEY UPDATE, I have come up with this, but am getting unknown column errors.

INSERT INTO mod_workorder_counts (asset_tag, wo_count) 
    (SELECT t.asset_tag, count(*) AS cnt 
        FROM mod_workorder_data t
        WHERE t.asset_tag IS NOT NULL 
     GROUP BY t.asset_tag)
ON DUPLICATE KEY UPDATE mod_workorder_counts.wo_count = t.cnt

When I run this I get #1054 - Unknown column 't.cnt' in 'field list'. I am not sure how to use the count values in the update.


回答1:


Once you reference it as "cnt", you no longer need the "t." portion. You should reference it as just "cnt".



来源:https://stackoverflow.com/questions/5687971/insert-select-count-on-duplicate-key-update-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!