简体   繁体   English

这个MySQL查询语法有什么问题?

[英]What is wrong with this MySQL query syntax?

$db = mysql_select_db("remote"); 

if(!$db) {
    die("Unable to select database");
} 

$query = "SET @cumulative_sum := 0; 
          SELECT timestamp, 
                 @cumulative_sum := @cumulative_sum + value AS cumulative_sum 
          FROM remote.historical 
          WHERE timestamp>= CURDATE()"; 

$result = mysql_query($qry);

When I use this query in my HeidiSQL, it output okay...running cumulative over current day, but when I copied this code into php file, web browser outputs: 当我在我的HeidiSQL中使用此查询时,它输出正常...在当天运行累积,但当我将此代码复制到php文件时,Web浏览器输出:

You have an error in your SQL syntax; 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT timestamp, @cumulative_sum := @cumulative_sum + value AS cumulative_sum F' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行的'SELECT timestamp,@ cum_sum:= @cumulative_sum + value AS cumulative_sum F'附近使用正确的语法

What is wrong with this code? 这段代码有什么问题?

Instead of executing two statements, you can alternative declare the variable in a subquery, eg. 您可以替代在子查询中声明变量,而不是执行两个语句,例如。

SELECT  timestamp, 
        @cumulative_sum := @cumulative_sum + value AS cumulative_sum 
FROM    remote.historical, (SELECT @cumulative_sum := 0) b
WHERE timestamp>= CURDATE()

For security reasons query() function do not allow multiple queries in the single statement but you can do this using mysqli_multi_query() . 出于安全原因,query()函数不允许在单个语句中进行多个查询,但您可以使用mysqli_multi_query()执行此操作。

The following link will help you: Multiple queries in single PHP query statement 以下链接将帮助您: 单个PHP查询语句中的多个查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM