表:User ( id, createdOn ) ——(id,创建时间)
situation #1
查出所有创建时间在2011年9月1日之后的记录。
# mysql (最简单了)
select * from user where createdOn>'2011-09-01'
# mysql的日期处理能力很强大,可以精确到年、月、日、时、分、秒、毫秒。
# 例如:
select * from user where createdOn>='2011-09'
会查出所有创建时间在2011年9月之后的记录。
#oracle
select * from user where trunc(createdOn)>trunc(to_date('2011-09-01', 'yyyy-MM-dd'))
# trunc() 是oracle的内置函数,它将日期数据转换为yyyy/MM/dd的格式。
更详细的内容可参考Trunc Function (with dates)或者TRUNC (date)。
附:MySQL 为日期增加一个时间间隔:date_add()
set @dt = now();
select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day); -- sub 1 day
situation #2
查出所有在2011年09月创建的记录。
# mysql
select * from user where createdOn>='2011-09' and createdOn<'2011-10'
#oracle
select * from user where trunc(createdOn, 'MONTH')=trunc(to_date('2011-10-1', 'yyyy-MM-dd'), 'MONTH')# trunc() 是oracle的内置函数。更详细的内容可参考
Trunc Function (with dates)或者
TRUNC (date)。。
hql
对于hql来说,日期是个头疼的问题,不过可以借助hql表达式(hql expressions)来做到。
比如第二种情况,
String hql = "from User where year(createdOn)=2011 and month(createdOn)=9 order by createdOn desc";关于hql表达式,详见
HQL Expressions。
1228

被折叠的 条评论
为什么被折叠?



