MySQL数据库日期操作

xiaoxiao2026-08-05  6

在MYSQL中,日期类型为 DATETIME   数据格式为: 2009-05-25 12:11:05

 

1. MySQL 官方tutorial 

11.6. Date and Time Functions

Link: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_adddate

 

2. MySQL DATE_ADD() Function

Link: http://www.w3schools.com/SQL/func_date_add.asp

 

-----------------------------------------------例子--------------------------------------------------------

问题1:搜索2007-08-25 的记录

select * from table where dt='2007-08-25'

 问题2:搜索2007-08-01到2007-08-15的所有记录

select * from table where dt between '2007-08-01' and '2007-08-15' 

between '2007-08-01' and '2007-08-15' 对该表中的dt仅能找出 2007-08-01 0:0:0 到 2007-08-15 0:0:0 之间的要实现具体的,自己修改修改

问题3:找出当前的日期减一天,或是减一个月

select * from table where abs(datediff(dt,current_timestamp))<=1 --前后一天内的 select * from table where period_diff(DATE_FORMAT(current_timestamp,'%Y%m'),DATE_FORMAT(dt,'%Y%m'))<1

 --------------------------------------------------------------------------------------------------------------

转载请注明原文地址: https://www.6miu.com/read-5050716.html

最新回复(0)