Answer the question
In order to leave comments, you need to log in
How to properly style INSERT ... ON DUPLICATE KEY UPDATE?
mariadb, yii2, how to properly style INSERT ... ON DUPLICATE KEY UPDATE?
tried like this:
\Yii::$app->db->createCommand()
->batchInsert(ProductsBarcodes::tableName(), ['product_code', 'barcode'], $barcodes_codes)
->execute();
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4064900053098' for key 'barcode'
$db = \Yii::$app->db;
$sql = $db->queryBuilder->batchInsert(ProductsBarcodes::tableName(), ['product_code', 'barcode'], $barcodes_codes);
$db->createCommand($sql . ' ON DUPLICATE KEY UPDATE')->execute();
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
The SQL being executed was: INSERT INTO `product_barcode` (`product_code`, `barcode`) VALUES ('123123', '123123123'), ('1005770', '873293005560'), ('1006274', '8001090190543'), ('1006275', '4015400681243'), ('1006276', '4015400681212'), ('1006277', '4606086374538') ON DUPLICATE KEY UPDATE
Answer the question
In order to leave comments, you need to log in
I did this:
1. I look for a record with the necessary attributes in the database
2. if there is - I skip it, if not - I add it to the array of updated records
3. I create a query that inserts records
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question