当前位置:数据库 > MySQL >>

mysql日期时间比较简单实例

 代码如下 复制代码

select * from tb where c> date_format('2007-07-06','%Y%m%d') and c <= date_format('2007-07-09','%Y%m%d');

select * from tb where c> date('2007-07-07') and c< date('2007-07-09')


STATDAY是形如2006031001的日期数据字段

 代码如下 复制代码

select * from LOGINSTAT where STATDAY> date_format(curdate()-1,'%Y%m%d') and STATDAY >= date_format(curdate(),'%Y%m%d');或者:select * from LOGINSTAT where STATDAY> date_format(curdate()-1,'%Y%m%d%H') and STATDAY >= date_format(curdate(),'%Y%m%d%H');


其他用法:

 代码如下 复制代码

select * from LOGINSTAT where STATDAY >= date_format('$date1','%Y%m%d%H') and STATDAY < date_format('$date','%Y%m%d%H')

   
mysql数据库中存的时间格式为2008-12-28 18:08:08,现在先要从一个一个结果集rs中获得一个日期时间。我先用rs.getDate()方法试着获取时间,结果只有年月日,无法获取小时、分和秒。最后解决的方法是:

 代码如下 复制代码

Date time1=new Date(rs.getTimestamp("pub_time").getTime());
SimpleDateFormat formattime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String pubtime=formatime.format(time1);

获得的pubtime为String型,在sql语句中用mysql的时间函数date_format('time','format')转换:

 代码如下 复制代码

String sqlstr="select * from do_document where pub_time<date_format('"+pubtime+"','%Y-%m-%d %H:%i:%s') order by pub_time desc limit 0,1";

然后执行该sql语句就查到了满足条件的记录

补充:数据库,mysql教程 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,