如何在mysql中更新多行?

时间:2016-12-31 10:16:23

标签: mysql mysql-5.7

我有一个桌面教练。 Table coach有这样的数据:

enter image description here

我想更新player_id列,以便数据如下所示:

enter image description here

type =“coach”的Player_id值取自type =“player”的id的值。它是基于字段信息中的值player_code与字段代码

之间的关系而获得的

我尝试使用查询自联接进行更新,如下所示:

UPDATE coach
SET player_id = (
            SELECT b.id
            FROM coach a
            LEFT JOIN coach b ON REPLACE(JSON_EXTRACT(b.information, "$.player_code"), '"', '') = b.code
            WHERE b.`type` = 'player'
          )
WHERE `type` = 'coach'

执行查询时,会出现如下错误:

Error Code: 1093
You can't specify target table 'coach' for update in FROM clause

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

update coach as c1
inner join (
    select b.id,
        b.code
    from coach b
    where b.`type` = 'player'
    ) as c2 on REPLACE(JSON_EXTRACT(c1.information, '$.player_code'), '"', '') = c2.code

set c1.player_id = c2.id
where c1.type = 'coach';