mysql - 简单的 MySQL 查询不在服务器上运行

标签 mysql ubuntu

我写了一个非常简单的查询

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/

相关文章:

php - 在ubuntu上部署laravel 5,得到白页

ubuntu - Docker-Compose 错误 : "line 1: Not: command not found" when executing --version (Current Version 1. 27.4)

bash - Systemctl bash 变量

ubuntu - Debian 打包 - 处理修改/创建的文件,debian/rules

php - Google 绘制来自 php 数组的连续 x 轴图表

python - 为什么这个 python 脚本不会输入到我设置的 MySQL 表中?

java - 线程 "main"java.sql.SQLException : No value specified for parameter 1 中的异常

java - 数据库查询以在单个查询中获取不同表的计数总和

ubuntu - 以root身份在脚本中运行命令/不要求输入密码

java - Spring - 无效参数值 : java. io.NotSerializableException