MySQL - insert into... on duplicate key update - 如何区分插入或更新?
MySQL - insert into... on duplicate key update - How to distinguish between insert or update?
我正在使用 Node.js。我使用 mysql
和 bluebird
包。
const pool = mysql.createPool({ ... });
const query = (stmt, params) => {
return Promise.promisify(pool.query, { context: pool })(stmt, params);
};
const params = { ... };
const stmt = 'insert into table set ? on duplicate key update ?';
return query(stmt, [params, params])
.then(results => {
// I want to know actually what is done, insert or update
});
return 对象中应该有一个键 affectedRows
。从引用看,affectedRows
插入时为1,更新时为0或2。
return query(stmt, [params, params])
.then(results => {
// I want to know actually what is done, insert or update
if (results.affectedRows === 1) {
// inserted
} else {
// updated
}
});
For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
参考:https://dev.mysql.com/doc/refman/8.0/en/mysql-affected-rows.html
虽然我对节点的这一点不那么精通api,但基本原理是一样的:
query(stmt, [params.col1, params.col2])
.then(results => {
Console.log("Affected rows: " + results.affectedRows);
});
现在,真正的问题是 MySQL 热衷于返回垃圾诊断信息。如果您修改超过 1 行,您将不知道每一行发生了什么,thanks to this:
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values.
如果你负担得起,一次一个地执行这些语句,并检查受影响的行数。否则,我正在挖掘一些 MySQL 内部函数,但我没有看到太多。
附带说明一下,您那里的通配符用得太过火了。相反,使用 space 到 update/insert 你想要的列,并参数化输入值:
-- Please don't name it 'table'
INSERT INTO my_table (column1, column2)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE column1 = VALUES(column1), column2 = VALUES(column2)
我正在使用 Node.js。我使用 mysql
和 bluebird
包。
const pool = mysql.createPool({ ... });
const query = (stmt, params) => {
return Promise.promisify(pool.query, { context: pool })(stmt, params);
};
const params = { ... };
const stmt = 'insert into table set ? on duplicate key update ?';
return query(stmt, [params, params])
.then(results => {
// I want to know actually what is done, insert or update
});
return 对象中应该有一个键 affectedRows
。从引用看,affectedRows
插入时为1,更新时为0或2。
return query(stmt, [params, params])
.then(results => {
// I want to know actually what is done, insert or update
if (results.affectedRows === 1) {
// inserted
} else {
// updated
}
});
For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag, the affected-rows value is 1 (not 0) if an existing row is set to its current values.
参考:https://dev.mysql.com/doc/refman/8.0/en/mysql-affected-rows.html
虽然我对节点的这一点不那么精通api,但基本原理是一样的:
query(stmt, [params.col1, params.col2])
.then(results => {
Console.log("Affected rows: " + results.affectedRows);
});
现在,真正的问题是 MySQL 热衷于返回垃圾诊断信息。如果您修改超过 1 行,您将不知道每一行发生了什么,thanks to this:
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values.
如果你负担得起,一次一个地执行这些语句,并检查受影响的行数。否则,我正在挖掘一些 MySQL 内部函数,但我没有看到太多。
附带说明一下,您那里的通配符用得太过火了。相反,使用 space 到 update/insert 你想要的列,并参数化输入值:
-- Please don't name it 'table'
INSERT INTO my_table (column1, column2)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE column1 = VALUES(column1), column2 = VALUES(column2)