Vous êtes sur la page 1sur 3

GettingtherecentonemonthoryearrecordsfromMySQLtable

Sometimewehavetocollectlast7or15daysorXdays(ormonth,yearorweek)datafromMySQLtable.

Forexampleletusfindoutwhoarethenewmembersjoinedinourforuminlastweek.Oneshopmaybe
interestedinknowingnewproductsaddedinlastonemonth.Whatarethebooksarrivedinlastoneyear.
HereirrespectiveofthedatevalueswewanttherecordsoflastXdaysfromtoday,orwecansaythatthe
recordsbetweentodayandlastXdays(month,yearorweek)arerequired.

WewillusetheMySQLfunctionCURDATE()togetthetoday'sdate.

TogetthedifferenceintodaydateandpreviousdayormonthwehavetousetheMySQL
functionDATE_SUB

DATE_SUBisaMySQLfunctionwhichtakesdateexpression,theintervalandtheconstanttoreturnthedate
valueforfurthercalculation.

Herearesomesamplequeriesonhowtogettherecordsasperrequirements.

Last10daysrecords

select*fromdt_tablewhere`date`>=DATE_SUB(CURDATE(),INTERVAL10DAY)

Theabovequerywillreturnlast10daysrecords.Notethatthisquerywillreturnallfuturedatesalso.To
excludefuturedateswehavetomodifytheabovecommandalittlebyusingbetweenquerytogetrecords.
Hereisthemodifiedone.

SELECT*FROMdt_tableWHERE`date`BETWEENDATE_SUB(CURDATE(),INTERVAL10DAY)ANDCURDATE()

Lastonemonthrecords
Letustrytogetrecordsaddedinlastonemonth

select*fromdt_tablewhere`date`>=DATE_SUB(CURDATE(),INTERVAL1MONTH)

HerealsofuturerecordswillbereturnedsowecantakecareofthatbyusingBETWEENcommandsif
required.

select*fromdt_tablewhere`date`>=DATE_SUB(CURDATE(),INTERVAL1YEAR)

Youcaneasilymakeoutwhattheabovequerywillreturn.

Recordsoftwodateranges
WecancollectrecordsbetweenaparticulardaterangesbyusingbetweencommandandDATE_SUB.Here
aresomequeriestogeneraterecordsbetweentwodateranges.

select*fromdt_tablewhere`date`BETWEENDATE_SUB(CURDATE(),INTERVAL3MONTH)ANDDATE_SUB(CURDATE()
,INTERVAL0MONTH)

Thisquerywillreturnrecordsbetweenlastthreemonths.Thisqueryagainwewillmodifytogettherecords
betweenthreemothsandsixmonths.

select*fromdt_tablewhere`date`BETWEENDATE_SUB(CURDATE(),INTERVAL6MONTH)ANDDATE_SUB(CURDATE()
,INTERVAL3MONTH)

Nowletuschangethistogetrecordsbetween6monthand12month.

select*fromdt_tablewhere`date`BETWEENDATE_SUB(CURDATE(),INTERVAL12MONTH)ANDDATE_SUB(CURDATE()
,INTERVAL6MONTH)

Withthisyoucanunderstandhowtherecordsbetweenamonthrangeorayearrangecanbecollectedfrom
atable.Notethatthemonthsrangesarecalculatedstartingfromcurrentday.Soifwearecollectingrecords

oflastthreemonthsandwearein15thdayof9thmonththenrecordsof15thdayof6thmonthwewillget
buttherecordsof14thdayof6thmonthwillbereturningonnextquerythatisbetween3monthsand6
months.

GenerateQuerybyusingdatesfromCalendar

Recordsoflastworkingdaysofweek
Nowletustryadifferentrequirement.Howtogettherecordsoftheworkingdaysoftheweeksofar?If
todayisThursdaythenrecordsfromMondaytoThursdayshouldbereturned.

Recordsofworkingdaysofthisweekorpreviousweek.

DynamicSQLdumpquery
Totestabovequeriesyouneedtohaverecordsoflast15daysorlast6months(or12Months).Anydump
createdtodaywillnotworkinfuturedates(sayafter8months).Sotouseabovequerywhichinvolveslast
15daysorlast12monthsrecords,wehavetogeneratedumpfileeveryday.

Vous aimerez peut-être aussi