Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带子查询的mysql更新查询_Mysql_Sql - Fatal编程技术网

带子查询的mysql更新查询

带子查询的mysql更新查询,mysql,sql,Mysql,Sql,有人能看到下面的查询有什么问题吗 当我运行它时,我得到: #1064-您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解要使用的正确语法 在“a”附近,其中a.CompetitionID=第8行的Competition.CompetitionID 主要问题是内部查询不能与外部update语句上的where子句相关,因为where过滤器在内部子查询执行之前首先应用于被更新的表。处理这种情况的典型方法是 演示:谢谢,我没有想到使用内部连接进行更新 在原始查询中,错误是命名子查询,

有人能看到下面的查询有什么问题吗

当我运行它时,我得到:

#1064-您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解要使用的正确语法 在“a”附近,其中a.CompetitionID=第8行的Competition.CompetitionID


主要问题是内部查询不能与外部
update
语句上的
where
子句相关,因为where过滤器在内部子查询执行之前首先应用于被更新的表。处理这种情况的典型方法是


演示:

谢谢,我没有想到使用内部连接进行更新

在原始查询中,错误是命名子查询,子查询必须返回一个值,因此不能使用别名

UPDATE Competition
SET Competition.NumberOfTeams =
(SELECT count(*) -- no column alias
  FROM PicksPoints
  WHERE UserCompetitionID is not NULL
  -- put the join condition INSIDE the subquery :
  AND CompetitionID =  Competition.CompetitionID
  group by CompetitionID
) -- no table alias
我们应该为每一项比赛记录做好准备

注意事项:

其效果与mellamokb提出的查询不完全相同,mellamokb不会在没有相应拾取点的情况下更新竞赛记录

由于
选择id,COUNT(*)GROUP BY id
将仅对现有id值进行计数

SELECT COUNT(*)
将始终返回一个值,如果未选择任何记录,则为0

这对你来说可能是个问题,也可能不是

支持0的mellamokb查询版本为:

Update Competition as C
LEFT join (
  select CompetitionId, count(*) as NumberOfTeams
  from PicksPoints as p
  where UserCompetitionID is not NULL
  group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = IFNULL(A.NumberOfTeams, 0)
换句话说,如果没有找到相应的拾取点,请将Competition.NumberOfTeams设置为零。

对于不耐烦的人:

UPDATE target AS t
INNER JOIN (
  SELECT s.id, COUNT(*) AS count
  FROM source_grouped AS s
  -- WHERE s.custom_condition IS (true)
  GROUP BY s.id
) AS aggregate ON aggregate.id = t.id
SET t.count = aggregate.count

这就是答案,如上所述,减少到最大值。

您可以检查eav_属性表,找到每个图像角色的相关属性ID,例如;

然后,您可以使用这些来为所有类似的产品将任何角色设置为任何其他角色

UPDATE catalog_product_entity_varchar AS `v` INNER JOIN (SELECT `value`,`entity_id` FROM `catalog_product_entity_varchar` WHERE `attribute_id`=86) AS `j` ON `j`.`entity_id`=`v`.entity_id SET `v`.`value`=j.`value` WHERE `v`.attribute_id = 85 AND `v`.`entity_id`=`j`.`entity_id`

以上内容将把您的所有“基本”角色设置为同一产品的“小”图像。

我必须执行以下操作:将tableA更新为内部联接tableB更新为A.tag=B.tag set A.tagId=B.id
UPDATE target AS t
INNER JOIN (
  SELECT s.id, COUNT(*) AS count
  FROM source_grouped AS s
  -- WHERE s.custom_condition IS (true)
  GROUP BY s.id
) AS aggregate ON aggregate.id = t.id
SET t.count = aggregate.count
UPDATE catalog_product_entity_varchar AS `v` INNER JOIN (SELECT `value`,`entity_id` FROM `catalog_product_entity_varchar` WHERE `attribute_id`=86) AS `j` ON `j`.`entity_id`=`v`.entity_id SET `v`.`value`=j.`value` WHERE `v`.attribute_id = 85 AND `v`.`entity_id`=`j`.`entity_id`