我写了一个非常简单的查询
UPDATE product_variants SET remaining= 15 WHERE id=(select id where remaining<15)在我的本地机器上成功地完成了工作。
但是,相同的查询在服务器上出现了以下错误
MySQL服务器版本,以便在第1行的“where remaining<15”附近使用正确的语法
我知道同样的查询也可以写成
UPDATE `product_variants` SET `remaining`= 15 WHERE remaining<15;但是我想知道第一个查询中的语法错误。
mysql --版本
mysql 14.14远端5.5.52,使用readline 6.3为debian gnu (x86_64)
Ubuntu版本
Linux 172-31-27-247 3.13.0-74-通用#118-Ubuntu清华12月17日22:52:10 UTC 2015 x86_64 GNU/Linux
发布于 2017-08-24 09:11:48
无法在子查询中的select语句中使用相同的update表,您可以在以下链接中找到原因:在子查询中不使用相同表的原因。
请尝试以下查询:
SET @r_ids = (select GROUP_CONCAT(id) FROM product_variants where remaining <15);
/* set the result id's into the one variable. */
SELECT @r_ids;
/* If you want to check the variable value, use above statement. */
UPDATE product_variants SET remaining= 15
WHERE id IN (@r_ids);
/* update that same id's which is find into the @r_ids. */您可以在这个链接中找到更多关于变量的信息。更多关于变量的内容。
首先,将id存储到某个变量中,然后使用in查询更新这些id。
发布于 2017-08-24 09:14:25
UPDATE product_variants SET remaining= 15 WHERE id=(select id where remaining<15)
两人认为:
(select GROUP_CONCAT(id) where remaining<15)缺少要从SELECT id FROM ... where remaining<15中选择的表where id in而不是where id =,因为查询可能返回多个行。这应该是可行的:
UPDATE product_variants
SET remaining= 15
WHERE id IN
(SELECT id
FROM
(SELECT id
FROM product_variants
WHERE remaining<15) a)https://stackoverflow.com/questions/45857486
复制相似问题