在MySQL中插入和更新单个行表的最佳方法是什么?
问题描述:
我有一个只包含一行的MySQL表.第一次插入此行以及以后的更新时,语句应该是什么?我尝试了一个主键等于1的插入,但这在没有行存在的情况下并没有第一次解决.
I have a MySQL table that will only have one row. What should my statement be for the first time I insert to this row, and for subsequent updates? I tried an insert where the primary key equals 1, but this doesn't account for the first time around when no row exists yet.
答
INSERT INTO table(col1,col2,col3) VALUES(val1,val2,val3) ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2, col3 = val3;