MySQL更新查询与子查询

编程入门 行业动态 更新时间:2024-10-24 01:57:13
本文介绍了MySQL更新查询与子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以看到以下查询出了什么问题吗?

Can anyone see what is wrong with the below query?

当我运行它时,我得到:

When I run it I get:

#1064-您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以使用正确的语法 在第8行的"a where a.CompetitionID = Competition.CompetitionID"附近

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where a.CompetitionID = Competition.CompetitionID' at line 8

Update Competition Set Competition.NumberOfTeams = ( SELECT count(*) as NumberOfTeams FROM PicksPoints where UserCompetitionID is not NULL group by CompetitionID ) a where a.CompetitionID = Competition.CompetitionID

推荐答案

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

The main issue is that the inner query cannot be related to your where clause on the outer update statement, because the where filter applies first to the table being updated before the inner subquery even executes. The typical way to handle a situation like this is a multi-table update.

Update Competition as C inner 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 = A.NumberOfTeams

演示: www.sqlfiddle/#!2/a74f3/1

更多推荐

MySQL更新查询与子查询

本文发布于:2023-06-01 10:28:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/412982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:与子   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!