Search code examples
mysqlnode.jsreplacenode-mysql

node-mysql: REPLACE into statement incrementing a current table value


I am using the MySQL driver for Node.js "node-mysql" but struggling with the syntax to do a REPLACE into and increment a counter. Here's what I have so far, note the count field is where I want to increment the current table value by 1:

connection.query('REPLACE INTO links SET ?', { symbol: statName, timestamp: now, count: count + 1  }, function(err, result) {

Can anyone advise on how to do this?


Solution

  • I found the solution in using INSERT with ON DUPLICATE KEY UPDATE:

    connection.query('INSERT INTO links SET symbol = ?, timestamp = ?, title = ?, url = ? ON DUPLICATE KEY UPDATE count = count + 1', [statName, now, interaction.data.links.title[y], interaction.data.links.url[y]], function(err, results) {
                    if (err) console.log(err);
                });