As for ON DUPLICATE KEY UPDATE...
, this is as simple as appending the clause to the builtend of the Joomla-generated query and including VALUES()
syntax. (only change onereplace the $db->setQuery()
line from the above snippet)
$db->setQuery($query . " ON DUPLICATE KEY UPDATE `date` = VALUES(`date`)");
to create this query:
INSERT INTO `#__tablename`
(`id`,`date`) VALUES
(79, '2018-12-01'),(78, '2018-10-01'),(76, '2018-06-01'),(80, '2019-01-01')
ON DUPLICATE KEY UPDATE `date` = VALUES(`date`)
When identical rows exist in the database table for all of the rows that you intend to add, you will see 0
affected rows. If the PRIMARY ID already exists -- let's say 1 row has a different date
value than what is in the query -- you will see 2
affected rows (I have run this on my own localhost and despite other things I've read, this must be removing, then adding). So this kind of information can be either very helpful or very confusing depending on what you are trying to determine. Imagine if you have all four id
s represented in your table already and two of the four rows need the date
value to be updated. After the query executes, you will be notified that 4
rows have been affected.
Here is a relevant answer that I posted a few years ago on StackOverflow.