mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法

2013-7-15 liyangweb.com 李杨 MySQL

 

mysql查询今天,昨天,近7天,近30天,本月,上一月数据的方法分析总结:

话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下:

1  select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');
或者:

1  select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
假设以上表的add_time字段的存储类型是DATETIME类型或者TIMESTAMP类型,则查询语句也可按如下写法:

查询今天的信息记录:

1  select * from `article` where to_days(`add_time`) = to_days(now());
查询昨天的信息记录:

1  select * from `article` where to_days(now()) – to_days(`add_time`) <= 1;
查询近7天的信息记录:

1  select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);
查询近30天的信息记录:

1  select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);
查询本月的信息记录:

1  select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
查询上一月的信息记录:

1  select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;
对上面的SQL语句中的几个函数做一下分析:

(1)to_days

就像它的名字一样,它是将具体的某一个日期或时间字符串转换到某一天所对应的unix时间戳,如:

01  mysql> select  to_days('2010-11-22 14:39:51');     
02   +--------------------------------+                                                       
03  | to_days('2010-11-22 14:39:51') |
04  +--------------------------------+
05  |                         734463 |
06  +--------------------------------+
07  
08  mysql> select  to_days('2010-11-23 14:39:51');
09  +--------------------------------+
10  | to_days('2010-11-23 14:39:51') |
11  +--------------------------------+
12  |                         734464 |
13  +--------------------------------+
可以看出22日与23日的差别就是,转换之后的数增加了1,这个粒度的查询是比较粗糙的,有时可能不能满足我们的查询要求,那么就需要使用细粒度的查询方法str_to_date函数了,下面将分析这个函数的用法。

提醒:

(1)to_days() 不用于阳历出现(1582)前的值,原因是当日历改变时,遗失的日期不会被考虑在内。因此对于1582 年之前的日期(或许在其它地区为下一年 ), 该函数的结果实不可靠的。
 
(2)MySQL"日期和时间类型"中的规则是将日期中的二位数年份值转化为四位。因此对于'1997-10-07'和'97-10-07'将被视为同样的日期:

1  mysql> select to_days('1997-10-07'), to_days('97-10-07');
2  
3  -> 729669, 729669
(2)str_to_date

这个函数可以把字符串时间完全的翻译过来,如:

1  mysql> select str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s');
2  
3  +--------------------------------------------------------+
4  | str_to_date("2010-11-23 14:39:51",'%Y-%m-%d %H:%i:%s') |
5  +--------------------------------------------------------+
6  | 2010-11-23 14:39:51                                    |
7  +--------------------------------------------------------+
具体案例操作如下:

1  select str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')
2  from article
3  where str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')>='2012-06-28 08:00:00' and str_to_date(article.`add_time`,'%Y-%m-%d %H:%i:%s')<='2012-06-28 09:59:59';

标签: mysql

评论:

allthatspool
2019-01-30 14:35
<a href="http://www.georgesherry.com/nike-free-3.0-v5-gr%C3%B8n-glow-jordans-scarpeb">nike free 3.0 v5 gr酶n glow jordans</a> <a href="http://www.jenalgroup.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.badacamshotel.com/air-jordan-future-mujeres-cielo-azul-kurpesb">air jordan future mujeres cielo azul</a> <a href="http://www.compressmyjs.com/nike-sandals-for-toddlers-zapatost">nike sandals for toddlers</a> <a href="http://www.fetison.com/girls-high-converse-shoesz">girls high converse</a> <a href="http://www.neffloot.com/air-jordan-13-retro-low-brave-bleu-uniform-schuhec">air jordan 13 retro low brave bleu uniform</a>
allthatspool http://www.allthatspool.com/
hulyatosun
2019-01-29 20:01
<a href="http://www.cosmocrafters.com/jordan-13-grigio-toe-gs-schuhef">jordan 13 grigio toe gs</a> <a href="http://www.dschocolate.com/nike-free-run-3-or-blanc-obuve">nike free run 3 or blanc</a> <a href="http://www.eucbowl.com/a-buon-mercato-lebron-13-tutti-star-nero-blu-butye">a buon mercato lebron 13 tutti star nero blu</a> <a href="http://www.gartnerhrinc.com/herren-salomon-xt-wings-3-rot-gr%C3%BCn-obuvd">herren salomon xt wings 3 rot gr眉n</a> <a href="http://www.akkadixllc.com/nike-lebron-11-gr%C3%A5-kurpesf">nike lebron 11 gr氓</a> <a href="http://www.beertapmap.com/new-blance-500-bambini-oro-viola-kurpesa">new blance 500 bambini oro viola</a>
hulyatosun http://www.hulyatosun.com/
dimaf
2019-01-27 19:31
<a href="http://www.gaialog.net/nike-internationalist-blu-legend-sunset-glow-butya">nike internationalist blu legend sunset glow</a> <a href="http://www.genopresse.net/nike-sb-dunk-low-pro-blanco-armada-butyc">nike sb dunk low pro blanco armada</a> <a href="http://www.gpsgolfcaddy.net/air-jordan-retro-11-enfants-rose-noir-scarpeg">air jordan retro 11 enfants rose noir</a> <a href="http://www.gregoryjewellers.net/kvinders-nike-air-max-tn-alle-guld-chaussuresb">kvinders nike air max tn alle guld</a> <a href="http://www.hamiltonbookkeeping.net/nike-air-max-zero-silber-bullet-blender-butya">nike air max zero silber bullet blender</a> <a href="http://www.handykontakte.net/kvinders-air-max-90-em-alle-gr%C3%B8n-schuhee">kvinders air max 90 em alle gr酶n</a>
dimaf http://www.dimaf.net/
linguatravels
2019-01-08 22:05
<a href="http://www.yolandapesto.com/soccers-scarpe-rosa-schuheb">soccers scarpe rosa</a> <a href="http://www.zovailangnews.com/nike-flyknit-air-max-oransje-gr%C3%B8nn-personality-butyf">nike flyknit air max oransje gr酶nn personality</a> <a href="http://www.dijitalbarter.com/lebron-12-sort-data-butyf">lebron 12 sort data</a> <a href="http://www.kristyhuston.com/adidas-prossoator-ace-zones-pro-obuvf">adidas prossoator ace zones pro</a> <a href="http://www.malibuj.com/nike-air-max2-cb-94-orange-schwarz-obuve">nike air max2 cb 94 orange schwarz</a> <a href="http://www.notbore.com/nike-air-max-90-hyperfuse-bianca-original-kurpesg">nike air max 90 hyperfuse bianca original</a>
linguatravels http://www.linguatravels.com/
ziabio
2019-01-07 08:28
<a href="http://www.willymunoz.com/nike-air-max-1-total-crimson-sail-squadron-blue-obsidian-trainersce">nike air max 1 total crimson sail squadron blue obsidian</a> <a href="http://www.seemeonweb.com/ray-ban-aviator-flash-lenses-cheap-sunglassesr">ray ban aviator flash lenses cheap</a> <a href="http://www.clojuregames.com/womens-mackage-coat-mackager">womens mackage coat</a> <a href="http://www.threeoaksmi.com/nike-roshe-run-hyp-menns-bl%C3%A5-sneakere">nike roshe run hyp menns bl氓</a>
ziabio http://www.ziabio.com/
2wbes304
2019-01-03 10:06
<a href="http://www.tvjuti65.com/nike-hypervenom-phantom-bleu-butye">nike hypervenom phantom bleu</a> <a href="http://www.pbyz412s.com/nike-kobe-11-svart-space-background-scarped">nike kobe 11 svart space background</a> <a href="http://www.x745mfcu.com/maschio-air-max-90-em-verde-arancia-schuhea">maschio air max 90 em verde arancia</a> <a href="http://www.dkouevy6.com/dam%C3%A4nner-adidas-zx-flux-beige-kurpesb">dam盲nner adidas zx flux beige</a> <a href="http://www.15wij42f.com/mujeres-nike-kyrie-1-oro-rojo-schuhef">mujeres nike kyrie 1 oro rojo</a> <a href="http://www.5k71d4ly.com/nike-kaishi-casual-schuhe-butye">nike kaishi casual schuhe</a>
2wbes304 http://www.2wbes304.com/
tritechems
2019-01-01 14:17
<a href="http://www.larrystoverjr.com/descuento-nike-air-max-china-butyd">descuento nike air max china</a> <a href="http://www.gokartinfo.com/lebron-soldier-9-bleu-and-blanc-game-obuvc">lebron soldier 9 bleu and blanc game</a> <a href="http://www.enemythunder.com/adidas-pr%C3%B8dator-lz-kopsport-scarped">adidas pr酶dator lz kopsport</a> <a href="http://www.lahtacentr.com/mennns-asics-gel-noosa-tri-7-bl%C3%A5-s%C3%B8lv-obuvc">mennns asics gel noosa tri 7 bl氓 s酶lv</a> <a href="http://www.intspv.com/herre-salomon-gcs-athletic-trail-lyser%C3%B8d-gul-chaussuresg">herre salomon gcs athletic trail lyser酶d gul</a> <a href="http://www.itsmylogo.com/nike-flyknit-racer-light-bleu-color-chaussuresa">nike flyknit racer light bleu color</a>
[url=http://www.tritechems.com/]tritechems[/url]
guntruther
2018-12-27 10:20
<a href="http://www.clearvieweps.com/mackage-jacket-aritzia-mackager">mackage jacket aritzia</a> <a href="http://www.offleaseford.com/roshe-run-palm-tree-exclusive-sportsb">roshe run palm tree exclusive</a> <a href="http://www.amyhessatx.com/billig-adidas-superstar-dam%C3%A4nner-schwarz-footc">billig adidas superstar dam盲nner schwarz</a> <a href="http://www.elindigo.com/nike-flyknit-roshe-run-wolf-grey-obsidian-only-sneakerc">nike flyknit roshe run wolf grey obsidian only</a> <a href="http://www.googleizate.com/red-mackage-jacket-mackager">red mackage jacket</a> <a href="http://www.offshoretent.com/adidas-shoes-for-men-and-women-skor">adidas shoes for men and women</a>
guntruther http://www.guntruther.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1