Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Collectingrecordsofcurrentdateorfromlastxseconds

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.

You might also like