Jump to content

select records within a date range


otuatail

Recommended Posts

Hi this is a dificult one for me. I want to select records from the last 3 days. ie

select * from table where Date > '2010-05-24 00:00:00' and Date < '2010-05-27 23:59:59'

 

I can get todays date and add 23:59:59. The problem is subtracting three days. is there any mysql function that can be used for the first date (today - 3 days)

 

Desmond.

 

Link to comment
Share on other sites

Ok I think I have nearly solved this.

This works

SELECT * 
FROM WorldCup
WHERE Date > DATE_ADD( CURDATE( ) , INTERVAL 15 
DAY ) 
AND Date < DATE_ADD( CURDATE( ) , INTERVAL 16 
DAY ) 

This dosn't

SELECT * 
FROM WorldCup
WHERE Date > DATE_ADD( CURDATE( ) +'00:00:00', INTERVAL 15 
DAY ) 
AND Date < DATE_ADD( CURDATE( ) + '23:59:59', INTERVAL 15 
DAY ) 

How do I get the hours into it as it will default to midnight.

Link to comment
Share on other sites

You could try ..

 

DAY(CURDATE()-3)

 

also works with

 

WEEK(CURDATE()-3)

MONTH(CURDATE()-3)

YEAR(CURDATE()-3)

 

Obviously change the "-3" to suit

And this won't work anyway -- you need INTERVAL for math.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.