mysql update query with sub query

前端 未结 3 1392
臣服心动
臣服心动 2020-11-30 23:46

Can anyone see what is wrong with the below query?

When I run it I get:

#1064 - You have an error in your SQL syntax; check the manual that co

3条回答
  •  悲哀的现实
    2020-12-01 00:19

    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
    

    Demo: http://www.sqlfiddle.com/#!2/a74f3/1

提交回复
热议问题