Javascript NodeJS中BETWEEN子句的使用
我想使用BETWEEN子句从MySql数据库中获取数据 在客户端,我用JQuery创建了2个内联日历,并用socket.io发出所选的日期值Javascript NodeJS中BETWEEN子句的使用,javascript,jquery,mysql,node.js,socket.io,Javascript,Jquery,Mysql,Node.js,Socket.io,我想使用BETWEEN子句从MySql数据库中获取数据 在客户端,我用JQuery创建了2个内联日历,并用socket.io发出所选的日期值 $('#fromDate').datepicker({ inline: true, altField: '#d', dateFormat: "dd-mm-yy", altFormat: "yy-mm-dd", monthNames: ["Ocak", "Şubat", "Mart", "Nisan", "Mayıs"
$('#fromDate').datepicker({
inline: true,
altField: '#d',
dateFormat: "dd-mm-yy",
altFormat: "yy-mm-dd",
monthNames: ["Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"],
dayNamesMin: ["Pa", "Pt", "Sl", "Ça", "Pe", "Cu", "Ct"],
firstDay: 1,
numberOfMonths: 1,
onSelect: function(dateText, inst) {
var dateAsString = dateText; //the first parameter of this function
var dateAsObject = $(this).datepicker('getDate'); //the getDate method
console.log(dateAsString);
socket.emit('socketfromDate', $('#d').val());
}
});
$('#toDate').datepicker({
inline: true,
altField: '#x',
dateFormat: "dd-mm-yy",
altFormat: "yy-mm-dd",
monthNames: ["Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"],
dayNamesMin: ["Pa", "Pt", "Sl", "Ça", "Pe", "Cu", "Ct"],
firstDay: 1,
numberOfMonths: 1,
onSelect: function(dateText, inst) {
var dateAsString = dateText; //the first parameter of this function
var dateAsObject = $(this).datepicker('getDate'); //the getDate method
console.log(dateAsString);
socket.emit('sockettoDate', $('#x').val());
}
});
在服务器端,如果我在“fromDate”值之后收到“toDate”值,MySql查询必须在这两天之间获取数据
var fromDate_query,
toDate_query;
io.on('connection', function(socket) {
socket.on('socketfromDate', function(fromDate) {
console.log();
console.log("----------------------------------");
console.log("Selected dateTime 'from': " + fromDate); //output: Selected dateTime 'from': 2017-07-01
console.log("----------------------------------");
console.log();
fromDate_query = fromDate;
});
socket.on('sockettoDate', function(toDate) {
console.log();
console.log("----------------------------------");
console.log("Selected dateTime 'to': " + toDate); //output: Selected dateTime 'to': 2017-07-13
console.log("----------------------------------");
console.log();
toDate_query = toDate;
connection.query("SELECT * FROM report WHERE date BETWEEN ? AND ?", [fromDate_query, toDate_query], function(err, rows) {
if (err) console.log(err);
else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
console.log("**********************************");
console.log("Possible machine: " + row.machine); //output: nothing
console.log("**********************************");
}
}
});
});
});
var fromDate\u查询,
toDate_查询;
io.on('连接',函数(套接字){
socket.on('socketfromDate',函数(fromDate){
console.log();
console.log(“---------------------------------------------”);
console.log(“选择的日期时间”from':“+fromDate);//输出:选择的日期时间“from”:2017-07-01
console.log(“---------------------------------------------”);
console.log();
fromDate\u query=fromDate;
});
socket.on('sockettoDate',函数(toDate){
console.log();
console.log(“---------------------------------------------”);
console.log(“所选日期时间”到“:”+toDate);//输出:所选日期时间“到”:2017-07-13
console.log(“---------------------------------------------”);
console.log();
toDate_query=toDate;
connection.query(“从报告中选择*日期介于?和之间?”,[fromDate\u query,toDate\u query],函数(err,行){
if(err)console.log(err);
否则{
对于(变量i=0;i
问题是,什么也没发生
在两天之间进行查询的最佳方法是什么?代码中有三个问题:
socket.on('dates', function(dateData) {
var toDate_query = dateData.toDate;
var fromDate_query = dateData.fromDate;
connection.query("SELECT * FROM report WHERE date BETWEEN ? AND ?", [fromDate_query, toDate_query], function(err, rows) {
if (err) console.log(err);
else {
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
console.log("**********************************");
console.log("Possible machine: " + row.machine); //output: nothing
console.log("**********************************");
}
}
});
socket.on('dates',函数(dateData){
var toDate_query=dateData.toDate;
var fromDate\u query=dateData.fromDate;
connection.query(“从报告中选择*日期介于?和之间?”,[fromDate\u query,toDate\u query],函数(err,行){
if(err)console.log(err);
否则{
对于(变量i=0;i
除此之外,您还必须重构前端,以便仅在两个日期都被选中时才发出事件
编辑:
奖励点->收到查询数据后,请释放SQL连接我无法重构客户端,但在我尝试的过程中,我发现了问题所在;我选择的日期没有记录,因此没有显示任何内容。:)另外,我不想刷新页面,这就是我使用socket.io的原因。感谢您的时间。@erayaras即使它确实有任何记录,您的代码也不会按预期工作。请仔细阅读我上面列出的要点&可能会阅读socket.io事件侦听器以及事件侦听器中对象的范围。