MySQL ON DUPLICATE KEY UPDATE 同时从查询中插入结果集

MySQL ON DUPLICATE KEY UPDATE while inserting a result set from a query(MySQL ON DUPLICATE KEY UPDATE 同时从查询中插入结果集)
本文介绍了MySQL ON DUPLICATE KEY UPDATE 同时从查询中插入结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 tableONE 查询并尝试将结果集插入 tableTWO.这有时会导致 tableTWO 中出现重复键错误.所以我想ON DUPLICATE KEY UPDATE 使用tableONE 结果集中的新确定值,而不是使用ON DUPLICATE KEY UPDATE columnA = columnA 来忽略它.

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'

推荐答案

问题是在重复键子句中不能使用任何分组函数(如COUNT.但是,有一个简单的解决这个问题的方法.您只需将 COUNT(crime_id) 调用的结果分配给一个变量,您可以在重复的键子句中使用该变量.然后您的插入语句将看起来像这样:

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;

我创建了一个 SQL Fiddle,向您展示它是如何工作的:SQL-Fiddle

I have create an SQL Fiddle that shows you how it works: SQL-Fiddle

您也可以使用 UPDATE crimecount = VALUES(crimecount) 并且没有变量:

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);

请参阅 SQL-Fiddle-2

See the SQL-Fiddle-2

这篇关于MySQL ON DUPLICATE KEY UPDATE 同时从查询中插入结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)