我写了一个非常简单的查询
UPDATE product_variants SET remaining= 15 WHERE id=(select id where remaining<15)
它在我的本地计算机上成功地完成了工作。
但是,相同的查询在服务器上出现以下错误
MySQL server version for the right syntax to use near 'where remaining<15)' at line 1
我知道同样的查询也可以写成
UPDATE `product_variants` SET `remaining`= 15 WHERE remaining<15;
但我想知道第一个查询中的语法错误。
mysql --版本
mysql Ver 14.14 Distrib 5.5.52, for debian-linux-gnu (x86_64) using readline 6.3
Ubuntu版本
Linux ip-172-31-27-247 3.13.0-74-generic #118-Ubuntu SMP Thu Dec 17 22:52:10 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
最佳答案
子查询中的 select 语句中无法使用相同的更新表,您可以在此链接中找到原因:Reason for not use same table in sub-query.
尝试以下查询:
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. */
您可以在此链接中找到有关变量的更多信息。 More about Variables.
首先将 id 存储到某个变量中,然后使用 in
查询更新这些 id。
关于mysql - 简单的 MySQL 查询不在服务器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45857486/