问题
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