Unix Technical Forum

Pulling data using NOW() and BETWEEN

This is a discussion on Pulling data using NOW() and BETWEEN within the MySQL forums, part of the Database Server Software category; --> I need more coffee..... I have a 'DATE' column [NOW()] so my records are timestamped. Can't quite figure out ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:54 AM
Akhenaten
 
Posts: n/a
Default Pulling data using NOW() and BETWEEN

I need more coffee.....

I have a 'DATE' column [NOW()] so my records are timestamped. Can't
quite figure out the correct query for pulling records between now and
a given period of time (24 hours, ten minutes, whatever...) What am I
missing for that last bit?


SELECT * FROM `my_table` WHERE `my_date` BETWEEN NOW() AND ???

TIA.
../JLK

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:54 AM
Captain Paralytic
 
Posts: n/a
Default Re: Pulling data using NOW() and BETWEEN

On 24 May, 11:58, Akhenaten <jonko...@gmail.com> wrote:
> I need more coffee.....
>
> I have a 'DATE' column [NOW()] so my records are timestamped. Can't
> quite figure out the correct query for pulling records between now and
> a given period of time (24 hours, ten minutes, whatever...) What am I
> missing for that last bit?
>
> SELECT * FROM `my_table` WHERE `my_date` BETWEEN NOW() AND ???
>
> TIA.
> ./JLK


It's not what you're missing from the last bit, it's the first bit
that's the problem.

A DATE column will only hold a date. To work with intervals of
minutes, you need a DATETIME or TIMESTAMP column. If you have a DATE
column, your records are date stamped, not timestamped. Once you have
a timestamp you can use the TIMESTAMPADD() function to get the
interval.

You will find details of this hidden away in the manual. The hide it
really well as they put it in the section called "Date and Time
Functions". Go figure. Could they have made it more difficult to find?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:54 AM
Bill Turczyn
 
Posts: n/a
Default Re: Pulling data using NOW() and BETWEEN

On May 24, 6:58 am, Akhenaten <jonko...@gmail.com> wrote:
> I need more coffee.....
>
> I have a 'DATE' column [NOW()] so my records are timestamped. Can't
> quite figure out the correct query for pulling records between now and
> a given period of time (24 hours, ten minutes, whatever...) What am I
> missing for that last bit?
>
> SELECT * FROM `my_table` WHERE `my_date` BETWEEN NOW() AND ???
>
> TIA.
> ./JLK


One of the ways to achieve this is to use the DATE_SUB function.

http://dev.mysql.com/doc/refman/5.0/...ction_date-sub

Nb.

select * from `my_table` where `my_date` > DATE_SUB(NOW(), INTERVAL 1
DAY)
and `my_date` <
DATE_SUB(NOW(), INTERVAL 1 HOUR) ;


Regards,
Bill

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:54 AM
Akhenaten
 
Posts: n/a
Default Re: Pulling data using NOW() and BETWEEN


>
> It's not what you're missing from the last bit, it's the first bit
> that's the problem.
>
> A DATE column will only hold a date. To work with intervals of
> minutes, you need a DATETIME or TIMESTAMP column. If you have a DATE
> column, your records are date stamped, not timestamped. Once you have
> a timestamp you can use the TIMESTAMPADD() function to get the
> interval.
>
> You will find details of this hidden away in the manual. The hide it
> really well as they put it in the section called "Date and Time
> Functions". Go figure. Could they have made it more difficult to find?



Correction - the column is in a timestamp format, just unsure as how
to reference my column as the date. Those nice little date/time
functions (ref'd here: http://dev.mysql.com/doc/refman/5.0/...functions.html)
are severely lacking in both explanation and example (IMHO).

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:54 AM
Akhenaten
 
Posts: n/a
Default Re: Pulling data using NOW() and BETWEEN

On May 24, 6:13 am, Bill Turczyn <bturc...@gmail.com> wrote:
> On May 24, 6:58 am, Akhenaten <jonko...@gmail.com> wrote:
>
> > I need more coffee.....

>
> > I have a 'DATE' column [NOW()] so my records are timestamped. Can't
> > quite figure out the correct query for pulling records between now and
> > a given period of time (24 hours, ten minutes, whatever...) What am I
> > missing for that last bit?

>
> > SELECT * FROM `my_table` WHERE `my_date` BETWEEN NOW() AND ???

>


>
> select * from `my_table` where `my_date` > DATE_SUB(NOW(), INTERVAL 1
> DAY)
> and `my_date` <
> DATE_SUB(NOW(), INTERVAL 1 HOUR) ;
>
> Regards,
> Bill


Thank you Bill...just the example I needed.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:53 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com