无法更新现有记录MySQL

时间:2018-10-31 15:54:23

标签: mysql

我创建了一个新列(第三列),所有记录都在该列中为null。该表有100行。当我尝试INSERT INTO时,将添加记录,但仅来自101行,而先前的记录仍为null。这就是为什么我决定使用UPDATE的原因,但是我收到了下一条错误消息:You can't specify target table 'actors' for update in FROM clause

这是我的查询:

UPDATE
    actors
SET
    starred_count = (
        SELECT COUNT(actors.Actor_id)
        FROM starred
        INNER JOIN actors ON actors.Actor_id = starred.Actor_id
        GROUP BY starred.Actor_id
        ORDER BY starred.Actor_id
    )

如何正确执行此操作?

1 个答案:

答案 0 :(得分:0)

尝试下一个查询,因为在MySQL上使用查询更新无效。

UPDATE
    actors AS a
INNER JOIN
    (SELECT Actor_id, COUNT(*) AS starred_count
     FROM starred
     GROUP BY Actor_id) AS s ON s.Actor_id = a.Actor_id
SET
    a.starred_count = s.starred_count