1

我有以下 PHP 代码:

$result = mysql_query('
            set @num := 0, @type := "";
            UPDATE orders INNER JOIN (
                SELECT id, user_id, created, row_number 
                FROM 
                (
                    SELECT id, user_id, created,
                    @num := if(@type = user_id, @num + 1, 1) AS row_number,
                    @type := user_id AS dummy
                    FROM orders
                    WHERE status = "queue" AND type="order"
                    ORDER BY user_id, created asc 
                ) AS grouped_orders 
                WHERE grouped_orders.row_number <= 2
            ) m ON orders.id = m.id 
            SET orders.status = "process", orders.lock_id = "hash";
        ');

当我跳过$result = mysql_query('并将');此查询复制粘贴到我的 PHP MyAdmin 面板时,它可以工作 - 但是当我使用 PHP 中的 mysql_query(...) 执行它时,我收到以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE orders INNER JOIN ( SELECT id, user_id, created, row_number FROM' at line 2

我试图用括号做一些事情,向上/向下移动并折叠到一行,但它不起作用......

不幸的是,在我正在处理的服务器上没有 mysqli。

4

2 回答 2

0

尝试这个:

$result = mysql_query('
            set @num := 0, @type := "";
            UPDATE orders INNER JOIN (
                SELECT id, user_id, created, row_number 
                FROM 
                (
                    SELECT id, user_id, created,
                    @num := if(@type = user_id, @num + 1, 1) AS row_number,
                    @type := user_id AS dummy
                    FROM orders
                    WHERE status = "queue" AND type="order"
                    ORDER BY user_id, created asc 
                ) AS grouped_orders 
                WHERE grouped_orders.row_number <= 2
            ) m ON orders.id = grouped_orders.id 
            SET orders.status = "process", orders.lock_id = "hash";
        ');

组顺序是要加入的第二个表的名称

编辑:

您的查询看起来没问题(至少没有错误),但是您的问题依赖于您使用 mysql_query 的 PHP。

“mysql_query() 向与指定链接标识符关联的服务器上当前活动的数据库发送一个唯一查询(不支持多个查询)。”(http://php.net/manual/en/function.mysql-query。 php)

所以你需要另一种方法来执行多查询,或者找到另一种解决方案,尝试强制使用mysqli,因为你也可以防止攻击并且能够进行多查询....

于 2012-06-07T12:43:08.187 回答
0

尝试多查询而不是mysql_query因为您正在执行多个查询

编辑

尝试在您的选择到期子选择中定义您的变量:

UPDATE orders INNER JOIN (
      SELECT id, user_id, created, row_number 
      FROM 
      (
           SELECT id, user_id, created,
           @num := if(@type = user_id, @num + 1, 1) AS row_number,
           @type := user_id AS dummy
           FROM orders
           WHERE status = "queue" AND type="order"
           ORDER BY user_id, created asc 
            ) AS grouped_orders, (SELECT @num:=0) r, (SELECT @type:="") t
            WHERE grouped_orders.row_number <= 2
        ) m ON orders.id = grouped_orders.id 
        SET orders.status = "process", orders.lock_id = "hash"; 
于 2012-06-07T12:45:02.423 回答