gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:12 25 4
gpt4 key购买 nike

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

"ERROR 1111 (HY000): Invalid use of group function".

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

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

最佳答案

我认为您不能在 ON DUPLICATE 中使用聚合函数. MySQL 看到你的 SQL 有点像这样:

insert into report_count_assets
expr
on duplicate key update
...

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

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement.

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

我不得不猜测 report_count_assetsproduct_id 列的名称。

如果这不起作用(显然它不起作用),那么您可以通过预先计算 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)

关于mysql - INSERT...SELECT...WHERE...ON DUPLICATE...mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com