CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   Selecting withing a range of timestamps (http://www.codingforums.com/showthread.php?t=274381)

RyanB88 09-27-2012 03:03 AM

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.

Old Pedant 09-27-2012 03:21 AM

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.

RyanB88 09-27-2012 03:30 AM

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.

Old Pedant 09-27-2012 03:41 AM

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


All times are GMT +1. The time now is 02:47 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.