Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2012, 03:03 AM   PM User | #1
RyanB88
Regular Coder

 
Join Date: Mar 2005
Location: Spokane, WA
Posts: 146
Thanks: 4
Thanked 3 Times in 3 Posts
RyanB88 is an unknown quantity at this point
Selecting withing a range of timestamps

I am trying to select records within a range of timestamps and I cannot get my query to work and was hoping someone could enlighten me as to why

Code:
SELECT * FROM database WHERE timestamp BETWEEN '2012-09-27 03:58:54' AND '2012-09-26 22:58:54'
Thanks in advance for your help.
RyanB88 is offline   Reply With Quote
Old 09-27-2012, 03:21 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,203
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
It looks right, except that since timestamp is a keyword in MySQL you need to enclose it in backticks. Oh...and database is also a keyword, so same problem.

I assume that your column named timestamp is indeed of data type timestamp?

Code:
SELECT * FROM `database` 
WHERE `timestamp` BETWEEN '2012-09-27 03:58:54' AND '2012-09-26 22:58:54'
*If* your timestamp column is actually a UNIX (Linux) timestamp--and therefore just an INT data type in your schema--then you have to use a function to convert it...or convert the two BETWEEN times to UNIX timestamps (which will be much more efficient if that field is indexed).

In the future, make life simpler for yourself: Don't use MySQL keywords for column names.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-27-2012, 03:30 AM   PM User | #3
RyanB88
Regular Coder

 
Join Date: Mar 2005
Location: Spokane, WA
Posts: 146
Thanks: 4
Thanked 3 Times in 3 Posts
RyanB88 is an unknown quantity at this point
I already though of that but renaming it to time_stamp did not solve the problem, neither did the `` for that matter.

The field is a timestamp type.
RyanB88 is offline   Reply With Quote
Old 09-27-2012, 03:41 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,203
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Silly me! I didn't notice before. Your two BETWEEN values are *BACKWARDS*!!!

The lower one MUST come first! (Which means earlier one in the case of timestamps.)

http://dev.mysql.com/doc/refman/5.5/...erator_between
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:57 PM.


Advertisement
Log in to turn off these ads.