比较SQL语句中的日期 [英] compare date in sql statement

查看:248
本文介绍了比较SQL语句中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web sql制作警报工具。

I am making an alert tool using web sql.

我想选择特定日期发生的所有事件。当我插入事件时,我将日期插入为具有 2011-10-14格式的日期对象,我应该在select语句中写什么以选择今天发生的所有事件?

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 ?

我这样子

 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);
});

其中我的查询是:

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

但是出现一条错误消息,即临近Oct:语法错误

but an error message occur which is "near Oct: syntax error"

我应该如何在sql中进行日期比较?

How should I make the date comparison in sql ??

推荐答案

我相信你想要:

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

请注意' 包装查询变量的值,然后调用 cur 日期对象的方法来构造日期。

Note the ' wrapping the value for the query variable, and calling the cur date object's methods to construct the date.

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/#introduction

这篇关于比较SQL语句中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆