我有以下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。
答案 0 :(得分: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";
');
组顺序是要加入的第二个表的名称
编辑:
你的查询看起来是O.K(至少没有错误),但你的问题依赖于PHP你正在使用mysql_query。
“mysql_query()向与指定link_identifier关联的服务器上的当前活动数据库发送唯一查询(不支持多个查询)。”(http://php.net/manual/en/function.mysql -query.php)
所以你需要另一种方法来执行多个查询,或者找到另一个解决方案,尝试强制使用mysqli,因为你也可以防止攻击,并且能够进行多次查询....
答案 1 :(得分:0)
尝试multi-query而不是mysql_query
,因为您正在执行更多的一个查询
尝试在select due subselects中定义变量:
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";