在 MySQL 中插入和更新单行表的最佳方法是什么?

2022-01-09 00:00:00 insert 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;

相关文章