node中使用msyql 对某个字段值进行+1操作
const mysql = require('mysql2');
// 创建连接池
const pool = mysql.createPool({
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
});
// 假设我们有一个名为 `clicks` 的表,其中有一个名为 `count` 的字段,我们现在想要对其加1。
// 我们还需要知道哪一行需要更新,因此假设有一个唯一标识符 `id`。
const updateQuery = 'UPDATE clicks SET count = count + 1 WHERE id = ?';
// 执行更新操作
pool.query(updateQuery, [specificId], (error, results, fields) => {
if (error) {
return console.error('Error executing query:', error);
}
// 检查受影响的行数
console.log('Changed rows:', results.affectedRows);
});
// 关闭连接池
pool.end();