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语句就查到了满足条件的记录

时间: 2024-07-30 19:12:41

mysql日期时间比较简单实例的相关文章

js格式化时间的简单实例_javascript技巧

Date.prototype.format = function(format) { //author: meizz let o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds

jsp页面中两种方法显示当前时间的简单实例_JSP编程

在jsp页面实现显示当前的日期时间,我们可以用一下两种方式实现: 1. 通过在jsp页面添加Java代码实现,主要代码如下所示 java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); java.util.Date currentTime = new java.util.Date(); String time = simpleDateForm

JavaScript实现页面实时显示当前时间的简单实例

这篇文章介绍了页面实时显示当前时间的简单实例,有需要的朋友可以参考需要   复制代码 代码如下: <html> <head> <title>JavaScript实现页面实时显示当前时间</title> </head> <body> <script language="javascript"> function showtime() { var today,hour,second,minute,year,

MySQL 日期时间 专题

1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp()   current_timestamp localtime()   localtime localtimestamp()   localtimestamp     这些日期时间函数,都等同于 now().鉴于 now() 函数简短易记,建议总是使用 now() 来替代上面列出的函数.   1.2 获得当前日期+

mysql日期时间常用函数总结

一.时间差 datediff:说白了就是用第一个时间去减第二个时间,顺序不能忘记  代码如下 复制代码 select datediff('2012-08-08', '2012-08-13'); -- -5 二.获取当前时间  代码如下 复制代码 now current_timestamp() ,current_timestamp ,localtime() ,localtime ,localtimestamp    -- (v4.0.6) ,localtimestamp()  -- (v4.0.6

MySQL日期时间函数

DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,--7=星期六,ODBC标准) mysql> select DAYOFWEEK('1998-02-03'); -> 3 WEEKDAY(date) 返回日期date是星期几(0=星期一,1=星期二,--6= 星期天). mysql> select WEEKDAY('1997-10-04 22:23:00'); -> 5 mysql> select WEEKDAY('1997-11-05'); -

Mysql日期时间函数大全

对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6 日期和时间类型.  这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内:  mysql> SELECT something FROM table  WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30; DAYOFWEEK(date)  返回日期date的星期索引(1=星期天,2=星期一, --7=星期六).这些索引值对应于ODBC

mysql中触发器的简单实例

 一个简单的例子 1.1. 创建表:    create table t(s1 integer); 1.2. 触发器:  代码如下 复制代码 ?delimiter | create trigger t_trigger before insert on t  for each row  begin set @x = "hello trigger";        set NEW.s1 = 55; end;  | 1.3. 如果触发器创建错误,可能只能删除了,至少我试过不能replace

PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)_php技巧

写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式. 一.在MySQL中完成 这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性. 1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME() 一般形式:selec