INSERT ... SELECT ... WHERE ... ON DUPLICATE ... mysql查询

时间:2012-02-04 06:57:24

标签: mysql mysql-error-1111

请帮忙。我正在尝试使用我们产品的当前资产数量来更新这一张表。如果产品已存在于表中,则应更新产品的最新更新计数。但是,使用下面的查询,mysql正在回复我

  

“ERROR 1111(HY000):无效使用组功能”。

我无法确定我的错误是什么,或者在重复键'的函数中使用count是否真的有效:

INSERT INTO report_count_assets
    SELECT products.product_id, 
    count(product_assets.asset_id),
    count(case when assets.asset_type_id=1 THEN 1 END), 
    count(case when assets.asset_type_id=2 THEN 1 END), 
    count(case when assets.asset_type_id=3 THEN 1 END), 
    count(case when assets.asset_type_id=11 THEN 1 END) 
    FROM products 
    LEFT JOIN product_assets USING (product_id) 
    LEFT JOIN assets USING (asset_id)
    WHERE products.brand_id=671

ON DUPLICATE KEY UPDATE
    asset_count = count(product_assets.asset_id),
    asset_type_image = count(case when assets.asset_type_id=1 THEN 1 END), 
    asset_type_video = count(case when assets.asset_type_id=2 THEN 1 END), 
    asset_type_sound = count(case when assets.asset_type_id=3 THEN 1 END), 
    asset_type_install = count(case when assets.asset_type_id=11 THEN 1 END);

2 个答案:

答案 0 :(得分:4)

我认为你不能在ON DUPLICATE中使用聚合函数。 MySQL看到你的SQL类似:

insert into report_count_assets
expr
on duplicate key update
...

ON DUPLICATE不知道expr中发生了什么,它只知道它有一个重复的行要处理。在不知道expr内部发生了什么的情况下,count没有上下文可供使用。此外,您应该在更新中使用values

  

您可以使用VALUES(col_name)子句中的UPDATE函数来引用INSERT语句的INSERT ... ON DUPLICATE KEY UPDATE部分中的列值。

并且values(count(x))不是有效的语法。但values(column_name)有效,所以这应该有效:

INSERT INTO report_count_assets
(product_id, asset_count, asset_type_image, asset_type_video, asset_type_sound, asset_type_install)
    SELECT products.product_id, 
    count(product_assets.asset_id),
    count(case when assets.asset_type_id=1 THEN 1 END), 
    count(case when assets.asset_type_id=2 THEN 1 END), 
    count(case when assets.asset_type_id=3 THEN 1 END), 
    count(case when assets.asset_type_id=11 THEN 1 END) 
    FROM products 
    LEFT JOIN product_assets USING (product_id) 
    LEFT JOIN assets USING (asset_id)
    WHERE products.brand_id=671
ON DUPLICATE KEY UPDATE
    asset_count = values(asset_count),
    asset_type_image = values(asset_type_image), 
    asset_type_video = values(asset_type_video), 
    asset_type_sound = values(asset_type_sound), 
    asset_type_install = values(asset_type_install);

我必须猜到product_idreport_count_assets列的名称。

如果这不起作用(显然它不起作用),那么你可以通过预先计算SELECT来做到这一点。创建一个临时表:

create temporary table t (
    product_id int,
    product_count int,
    assets1 int,
    assets2 int,
    assets3 int,
    assets11 int
)

填充它:

INSERT INTO t (product_id, product_count, assets1, assets2, assets3, assets11)
SELECT products.product_id, 
count(product_assets.asset_id),
count(case when assets.asset_type_id=1 THEN 1 END), 
count(case when assets.asset_type_id=2 THEN 1 END), 
count(case when assets.asset_type_id=3 THEN 1 END), 
count(case when assets.asset_type_id=11 THEN 1 END) 
FROM products 
LEFT JOIN product_assets USING (product_id) 
LEFT JOIN assets USING (asset_id)
WHERE products.brand_id=671

然后使用该临时表来执行您真正想要执行的插入操作:

insert into report_count_assets
    select product_id, product_count, assets1, assets2, assets3, assets11
    from t
on duplicate key update
    asset_count = values(product_count),
    asset_type_image = values(assets1),
    asset_type_video = values(assets2),
    asset_type_sound = values(assets3),
    asset_type_install = values(assets11)

答案 1 :(得分:0)

尝试按product_assets.asset_id进行分组:

.
.
WHERE products.brand_id=671
group by product_assets.asset_id
ON DUPLICATE KEY UPDATE
    asset_count = count(product_assets.asset_id)
.
.