子查询内部的MySQL限制会产生错误

时间:2012-09-29 20:53:20

标签: mysql

我收到了:

[Err] 1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

执行以下查询时:

UPDATE account.account
SET STATUS = 'BLOCK'
WHERE
    id IN (
        SELECT
            p.account_id
        FROM
            log.log
        LEFT JOIN player.player p ON (p.id = log.who)
        WHERE
            vnum = 71054
        AND how = 'BUY'
        GROUP BY
            log.`who`
        HAVING
            COUNT(log.who) > 2
        LIMIT 10
    );

是否有可能重写此查询以便MySQL可以执行它?

1 个答案:

答案 0 :(得分:4)

解决方案是加入子查询而不是使用IN()INNER JOIN只返回来自有限子查询的account匹配ID中的行。然后可以在没有UPDATE子句的情况下执行WHERE

Update 
  account.account AS account
  INNER JOIN (
        SELECT
            p.account_id
        FROM
            log.log
            LEFT JOIN player.player p ON (p.id = log.who)
        WHERE
            vnum = 71054
            AND how = 'BUY'
        GROUP BY log.`who`
        HAVING COUNT(log.who) > 2
        LIMIT 10 
   ) subq ON account.id = subq.id
SET STATUS='BLOCK'

要验证要修改的行,请先使用SELECT

SELECT
  account.*
FROM 
  account.account
  INNER JOIN (
            SELECT
                p.account_id
            FROM
                log.log
                LEFT JOIN player.player p ON (p.id = log.who)
            WHERE
                vnum = 71054
                AND how = 'BUY'
            GROUP BY log.`who`
            HAVING COUNT(log.who) > 2
            LIMIT 10 
       ) subq ON account.id = subq.id