11

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'
2
  • 1
    so the alias doesn't work. Use count(crime_id)? Commented Jun 5, 2013 at 9:13
  • says Invalid use of group function Commented Jun 5, 2013 at 11:11

1 Answer 1

28

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

Sign up to request clarification or add additional context in comments.

5 Comments

PERFECT :) .... However, in the SQL Fiddle, i think you forgot to add date , city AS the PRIMARY KEY... ALTER TABLE simple_crimecount` ADD PRIMARY KEY (date , city);` otherwise, it's not finding any DUPLICATE KEY at all... I am editing your answer with the update SQLfiddle.... thanks man :)
You're welcome! I did forget to add the primary key, feel free to edit my post.
You can also use ON DUPLICATE KEY UPDATE crimecount = VALUES(crimecount). See the Fiddle-2
hey @ypercube, thanks for the update.... your update is worth being posted as a separate answer.... I think fiddle2 is the more recommended way to do such an update in case of a ON DUPLICATE KEY according to the MySQL developer documentation - dev.mysql.com/doc/refman/5.0/en/…
When using the first approach with setting a variable in the select statement (@determined_crimecount := count(crime_id)) I get the following warning in MySQL 8.0.16: 1287 Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expression, ...', or 'SELECT expression(s) INTO variables(s)' - for this reason, I opted with using the second approach by using VALUES(crimecount) in the update. This worked correctly and I safely avoided deprecation warnings. +1 for your helpful answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.