开发者

MySQL bulk insert (ignoring primary key id, but on duplicate update another key)

On a bulk insert, how do I ignore a duplicate key update for primary key 'id' while only updating the entry for another key 'another_unique_id'?

INSERT INTO
  some_table (`id`, `another_unique_id`, `raw_data)
  VALUES     (5   ,  'ABCDEFG', 'blah')
ON DUPLICATE KEY UPDATE ...?

For example - If there's a record with id: 5, I don't want to update it. I'll just insert a new record.

If there's a re开发者_如何学JAVAcord with 'ABCDEFG' in the 'another_unique_id' field, then I'd like to update that entry to 'blah'


I think you should pass

INSERT INTO
some_table (`id`, `another_unique_id`, `raw_data)
VALUES     (NULL   ,  'ABCDEFG', 'blah')
ON DUPLICATE KEY UPDATE ...?

otherwise it doesn't make sense


At least on mysql you can use:

INSERT IGNORE INTO
some_table (`id`, `another_unique_id`, `raw_data`)
VALUES     (5 ,  'ABCDEFG', 'blah')

If a field with id 5 exists it will do the same as an update, and if it doesn't exist, it will do the same as the regular insert.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜