How can I get this? I know I can do this by getting COUNT of all rows before running this Insert query, and then substract it from COUNT of all rows after running this Insert query, but it's not practical for big databases. I also found this formula on stackoverflow.com
(numberOfValuesInInsert * 2) - mysql_affected_rows()
but it doesn't seem to work for me, as I get affected rows = 1 no matter if I update or insert new row
Here is my node.js code running inside an FOR loop, so after ending of loop I get sum of affected rows:
var sql = "INSERT INTO classdata (id, siteid, name, staff, locationname, start, end) VALUES ?
ON DUPLICATE KEY UPDATE
siteid=VALUES(siteid), name=VALUES(name), staff=VALUES(staff), locationname=VALUES(locationname), start=VALUES(start), end=VALUES(end)";
var values = [[id, siteId, name, staff, locationName, start,end]];
connection.query(sql, [values],function(err, results) {
if (!err) {console.log(new Date() + " : New data sucessfully inserted into classdata table. Affected rows: " + results.affectedRows);}});
affectedrows+= results.affectedRows;
And it always prints Affected rows: 1, no matter if record is inserted or updated..And I read here that it should return 2 for each updated record and 1 for inserted. Getting number of rows inserted for ON DUPLICATE KEY UPDATE multiple insert?
I have one PK, it is "id" field if that means something to you.