简体   繁体   English

Node.js MySQL查询语法,用于检索

[英]Node.js mysql query syntax for retrieving

I am using nodejs with MySQL nom to retrieve date. 我正在使用带有MySQL nom的nodejs来检索日期。 currently select * from table works fine but if I want to query db and retrieve date between 2 values it's not working. 当前select * from table可以正常工作,但是如果我想查询db并检索2个值之间的日期,则无法正常工作。

The node code is 节点代码是

app.get('/api',function(req,res)
{


  var startDate = req.param('startDate');
  var endDate = req.param('endDate');
  var sqlQuery =null;
  var message ={};
  message.status=false;

  log("Params Found Changing SQL Queries For Start And End Date");
    sqlQuery ="SELECT * from sentiment_data where *file_ts* >= "+startDate+" and *file_ts* <= "+endDate+" order by file_ts";
    log(sqlQuery);
    if(user.isDB)
    {  
      connection.query(sqlQuery, function(err, rows, fields)
      {
        if (!err)
        {
          message = rows;
          res.send(message);
        }
      });
    }
    else
    {
      log("DB Error");
    }

});

The SQL statement I am executing when building it with start time and end time is 我在使用开始时间和结束时间进行构建时执行的SQL语句是

SELECT * from sentiment_data 
 where *file_ts* >= "+startDate+" 
  and *file_ts* <= "+endDate+" 
 order by file_ts

I am building this query and its not working. 我正在建立此查询,它不起作用。

*file_ts* is not valid SQL; *file_ts*是无效的SQL; nor is it possible to just plonk an unquoted date into a query. 也不能仅将未引用的日期填入查询中。 Use parameter binding; 使用参数绑定; it will also protect you from Bobby Tables . 它还可以保护您免受Bobby Tables的侵害。

var sqlQuery = "SELECT * FROM sentiment_data WHERE file_ts >= ? AND file_ts <= ? ORDER BY file_ts";
// ...
connection.query(sqlQuery, [startDate, endDate], function(err, results) {
  // ...
});

And depending on what is in startDate and endDate , you might need to use Date.parse to make them understandable. 根据startDateendDate ,您可能需要使用Date.parse使它们易于理解。

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

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