在重复密钥更新上与插入相同 [英] On Duplicate Key Update same as insert
问题描述
我已经搜索过了,但是没有找到可能.
I've searched around but didn't find if it's possible.
我有这个MySQL查询:
I've this MySQL query:
INSERT INTO table (id,a,b,c,d,e,f,g) VALUES (1,2,3,4,5,6,7,8)
字段ID具有唯一索引",因此不能有两个.现在,如果数据库中已经存在相同的ID,我想对其进行更新.但是我真的必须再次指定所有这些字段吗?
Field id has a "unique index", so there can't be two of them. Now if the same id is already present in the database, I'd like to update it. But do I really have to specify all these field again, like:
INSERT INTO table (id,a,b,c,d,e,f,g) VALUES (1,2,3,4,5,6,7,8)
ON DUPLICATE KEY UPDATE a=2,b=3,c=4,d=5,e=6,f=7,g=8
或者:
INSERT INTO table (id,a,b,c,d,e,f,g) VALUES (1,2,3,4,5,6,7,8)
ON DUPLICATE KEY UPDATE a=VALUES(a),b=VALUES(b),c=VALUES(c),d=VALUES(d),e=VALUES(e),f=VALUES(f),g=VALUES(g)
我已经指定了插入内容中的所有内容...
I've specified everything already in the insert...
需要特别说明的是,我想通过变通的方式获得ID!
A extra note, I'd like to use the work around to get the ID to!
id=LAST_INSERT_ID(id)
我希望有人能告诉我最有效的方法是什么.
I hope somebody can tell me what the most efficient way is.
推荐答案
没有其他方法,我必须两次指定所有内容.首先是插入,第二是更新.
There is no other way, I have to specify everything twice. First for the insert, second in the update case.
这篇关于在重复密钥更新上与插入相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!