Maison > base de données > tutoriel mysql > le corps du texte

[转]mysql查询今日,昨天,近7天,近30天,本月,上一月数据方法

WBOY
Libérer: 2016-06-07 16:26:36
original
1444 Les gens l'ont consulté

[转]mysql查询今天,昨天,近7天,近30天,本月,上一月数据方法 01 话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为 int (5)类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下: 02 ? 03 1??? select * f

[转]mysql查询今天,昨天,近7天,近30天,本月,上一月数据方法
01 话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下:
02 ?
03 1??? select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');
04 或者:
05 ?
06 1??? select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
07 假设以上表的add_time字段的存储类型是DATETIME类型或者TIMESTAMP类型,则查询语句也可按如下写法:
08 ?
09 查询今天的信息记录:
10 ?
11 1??? select * from `article` where to_days(`add_time`) = to_days(now());
12 查询昨天的信息记录:
13 ?
14 1??? select * from `article` where to_days(now()) – to_days(`add_time`)
15 查询近7天的信息记录:
16 ?
17 1??? select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <code class="keyword">date(`add_time`);
18 查询近30天的信息记录:
19 ?
20 1??? select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <code class="keyword">date(`add_time`);
21 查询本月的信息记录:
22 ?
23 1??? select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
24 查询上一月的信息记录:
25 ?
26 1??? select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;
27 对上面的SQL语句中的几个函数做一下分析:
28 ?
29 (1)to_days
30 ?
31 就像它的名字一样,它是将具体的某一个日期或时间字符串转换到某一天所对应的unix时间戳,如:
32 ?
33 01?? mysql> select? to_days('2010-11-22 14:39:51');?????
34 02??? +--------------------------------+???????????????????????????????????????????????????????
35 03?? | to_days('2010-11-22 14:39:51') |
36 04?? +--------------------------------+
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!