多子查询计数功能无法承受多次选择的压力

时间:2012-10-06 15:23:56

标签: mysql sql count

这让我很难过。

的MySQL

UPDATE sets SET sets.current_count = (SELECT COUNT(leads_auto.set_id) AS current_count FROM leads_auto WHERE leads_auto.set_id =  (SELECT sets.set_id AS setID FROM sets WHERE sets.on_off = 0)) WHERE sets.on_off = 0
似乎没错呢?更新记录current_count,其中leads_auto中的总行数set_id set_id sets表的值on_off {1}}列为0.

但我收到此错误

#1093 - You can't specify target table 'sets' for update in FROM clause

我环顾四周,有人提到它与循环操作有关吗?

1 个答案:

答案 0 :(得分:2)

SET

的结果创建临时表
UPDATE sets
SET sets.current_count = 
                (
                SELECT COUNT(leads_auto.set_id) AS current_count
                FROM leads_auto
                WHERE leads_auto.set_id = 
                                (
                                    SELECT set_id
                                    FROM
                                    (
                                        SELECT sets.set_id AS setID
                                        FROM sets
                                        WHERE sets.on_off = 0
                                    ) c
                                )
                )
WHERE sets.on_off = 0