I am querying from tableONE and trying to insert the result set into tableTWO. This can cause a duplicate key error in tableTWO at times. So i want to ON DUPLICATE KEY UPDATE
with the NEW determined value from the tableONE result set instead of ignoring it with ON DUPLICATE KEY UPDATE columnA = columnA
.
INSERT INTO `simple_crimecount` (`date` , `city` , `crimecount`)(
SELECT
`date`,
`city`,
count(`crime_id`) AS `determined_crimecount`
FROM `big_log_of_crimes`
GROUP BY `date`, `city`
) ON DUPLICATE KEY UPDATE `crimecount` = `determined_crimecount`;
# instead of [ON DUPLICATE KEY UPDATE `crimecount` = `crimecount`];
It returns an error saying the following
Unknown column 'determined_crimecount' in 'field list'
ON DUPLICATE KEY UPDATE inserts or updates a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas. The use of VALUES() to refer to the new row and columns is deprecated beginning with MySQL 8.0.
INSERT ... ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.
Preventing Duplicates from Occurring in a Table. You can use a PRIMARY KEY or a UNIQUE Index on a table with the appropriate fields to stop duplicate records. Let us take an example – The following table contains no such index or primary key, so it would allow duplicate records for first_name and last_name.
The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.
The problem is that in the duplicate key clauses you cannot use any grouping functions (such as COUNT
. However, there is an easy way around this problem. You just assign the result of the COUNT(crime_id)
call to a variable, which you can use in the duplicate key clauses. Your insert statement would then look like this:
INSERT INTO `simple_crimecount` (`date` , `city` , `crimecount`)(
SELECT
`date`,
`city`,
@determined_crimecount := count(`crime_id`) AS `determined_crimecount`
FROM `big_log_of_crimes`
GROUP BY `date`, `city`
) ON DUPLICATE KEY UPDATE `crimecount` = @determined_crimecount;
I have create an SQL Fiddle that shows you how it works: SQL-Fiddle
You could also use UPDATE crimecount = VALUES(crimecount)
and no variables:
INSERT INTO `simple_crimecount` (`date` , `city` , `crimecount`)(
SELECT
`date`,
`city`,
count(`crime_id`) AS `determined_crimecount`
FROM `big_log_of_crimes`
GROUP BY `date`, `city`
) ON DUPLICATE KEY UPDATE `crimecount` = VALUES(crimecount);
See the SQL-Fiddle-2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With