Use of insertOnDuplicate query using Magento 2, You need to create a Connection object first to run the query using ResourceConnection class.
When you insert any new row into a table if the row causes a duplicate in the Primary key or UNIQUE index, throw an error in MySQL.
Base Definition of function:
/**
* Inserts a table row with specified data.
*
* @param mixed $table The table to insert data into.
* @param array $data Column-value pairs or array of column-value pairs.
* @param array $fields update fields pairs or values
* @return int The number of affected rows.
*/
public function insertOnDuplicate($table, array $data, array $fields = []);
Example,