Professional Documents
Culture Documents
Collecting Records of Current Date or From Last X Seconds
Collecting Records of Current Date or From Last X Seconds
Wewilltrytogetallrecordsinsertedorupdatedtoday.Wewillusethedateandtimefieldwhichstoresthedate
andtimedataoftherecords.WhilecomparingwewilluseMySQLcurdate()functionwhichreturnstoday'sdate.
Hereisthequery.
Today'srecords
SELECT*FROM`test_time2`WHEREtm2=curdate()
Hereourtmfieldstoresdatedataonly.Ifwearestoringdateandtimeboththenwehavetochangethequeryby
addingagreaterthanequaltocomparison.
SELECT*FROM`test_time`WHEREtm>=curdate()
Thiswaywecancollectrecordsofpresentdate.WecanuseDATE_SUB()functionstogettherecordsoflast7days
or15daysorXdays.
Gettingallrecordsoflast10minutesor5secondsorlastonehour.
Wewillbeusingunix_timestamp()whichgivesoutputinSecondselapsedsince1stJan1970
HereisthequerytocollectrecordsaddedorupdatedinlastXseconds.
SELECT*FROMtest_time2WHERE(unix_timestamp()unix_timestamp(tm))<5
Theabovequerywillreturnallrecordsupdatedwithinlastfiveseconds.ThiscanbechangedtoX(any)seconds.
Youcanmodifytheabovequerytoreturnrecordsoflastonehouroranytimemultiplesinseconds.
Thisisrequiredinpreventingspampostings(inforumsetc.)whichautomaticallyaddsnewpostsorreplies.We
cancheckthisbystoppingpostedwithinaparticularduration.