简体   繁体   English

比较SQL语句中的日期

[英]compare date in sql statement

I am making an alert tool using web sql. 我正在使用Web sql制作警报工具。

I want to selects all events that occur in a specific day. 我想选择特定日期发生的所有事件。 when I insert the event I insert the date as a date object with this format "2011-10-14" , What should I write in select statement to select all events that occur today ? 当我插入事件时,我将日期插入为具有“ 2011-10-14”格式的日期对象, 我应该在select语句中写什么以选择今天发生的所有事件?

I make like this 我像这样

 cur =new Date();
alert(cur);
db.transaction(function(tx){
    tx.executeSql("select NOTE_DESC,NOTE_DATE,NOTE_TIME,ALERT_TIME,NOTES_MORE_DETAILS from NOTES where NOTE_DATE = "+cur+" order by NOTE_TIME asc limit 0,1",[], alert("yes"),onError);
});

In which my query is: 我的查询在其中:

select NOTE_DESC, NOTE_DATE, NOTE_TIME, ALERT_TIME, NOTES_MORE_DETAILS 
from NOTES 
where NOTE_DATE = "+cur+" 
order by NOTE_TIME asc 
limit 0,1

but an error message occur which is "near Oct: syntax error" 但是出现一条错误消息,它是“接近十月:语法错误”

How should I make the date comparison in sql ?? 我应该如何在sql中进行日期比较?

I believe you want: 我相信你想要:

NOTE_DATE = '"+cur.getFullYear()+'-'+(cur.getMonth()+1)+'-'+cur.getDate()+"'

Note the ' wrapping the value for the query variable, and calling the cur date object's methods to construct the date. 注意'包装查询变量的值,并调用cur date对象的方法构造日期。

http://jsfiddle.net/yXgDw/ http://jsfiddle.net/yXgDw/

Better yet, use a prepared query: 更好的是,使用准备好的查询:

http://www.w3.org/TR/webdatabase/#dom-sqltransaction-executesql http://www.w3.org/TR/webdatabase/#dom-sqltransaction-executesql

http://www.w3.org/TR/webdatabase/#introduction http://www.w3.org/TR/webdatabase/#introduction

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

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