实现查询出艾宾浩斯记忆规律时间点的数据

SELECT id,from_unixtime(pubdate) FROM sq_question WHERE TO_DAYS( NOW( ) ) - TO_DAYS(from_unixtime(pubdate)) IN (1,2,4,7,15,30);

今天

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

近7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '



上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '



查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

查询上季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'



查询上周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'



查询上个月的数据

select name,submittime from enterprise where date_format(submittime,'



查询当前月份的数据 

select name,submittime from enterprise   where date_format(submittime,'



查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

Leave A Comment

Recommended Posts

TIMESTAMP转为datetime格式

UPDATE sq_question_content as cINNER JOIN sq_question as s ON c.question_id = s.idSETc.create_at = FROM_UNIXTIME(s.pubdate) ,c.update_at = FROM_UNIXTIME(s.pubdate); 这个错误的核心原因是:sq_question.pubdate 并非 MySQL 原生的 TIMESTAMP 类型,而是以整数(Unix 时间戳,秒数)形式存储的数值(如 1459849198 是 2016年4月5日的 Unix 时间戳),直接用 CAST(…) AS DATETIME 无法将数字转为合法的 datetime 格式,因此触发 1292 错误。 解决方案:使用 FROM_UNIXTIME() 函数转换 Unix 时间戳 […]

blueidea

Mysql处理时间及字符串

1、mysql查询条件自动去除左边的0 处理办法:字符串+0,可以自动转换 2、计算时间差 SELECT DATEDIFF(‘2008-8-21,’2009-3-21’); 跟SqlServer 差不多 3、 在Mysql 数据库中存在两种字符串连接操作.具体操作如下 : CONCAT(string1,string2,…) 说明 : string1,string2代表字符串,concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL 例1: 例2: CONCAT_WS(separator,str1,str2,…) 说明 : string1,string2代表字符串,concat_ws 代表 concat with separator,第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。 举例1: mysql> select concat_ws(‘#’,’courseName=’,’NX’,null) AS nx_courseName; MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC […]

blueidea