0% found this document useful (0 votes)
43 views1 page

Collecting Records of Current Date or From Last X Seconds

Collecting Records of Current Date or From Last x Seconds

Uploaded by

Walter Buyela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views1 page

Collecting Records of Current Date or From Last X Seconds

Collecting Records of Current Date or From Last x Seconds

Uploaded by

Walter Buyela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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