mjacobson
Posts: 204 Joined: Fri Feb 08, 2002 3:35 pm
Post
by mjacobson » Mon Jul 21, 2008 3:19 pm
I am trying to select all records of a table that the id has been created within the last 10 days. Would the following work?
select id from mytable where id > '10 days'
or is there a better select statement? Should I have an index on id to make this more efficient and if so, what type of index?
mark
Site Admin
Posts: 5519 Joined: Tue Apr 25, 2000 6:56 pm
Post
by mark » Mon Jul 21, 2008 3:44 pm
You want
where id > '-10 days'
and a regular index on id to speed it up.
mjacobson
Posts: 204 Joined: Fri Feb 08, 2002 3:35 pm
Post
by mjacobson » Mon Jul 21, 2008 4:07 pm
So my select is OK, I just need to put an index on it.
John
Site Admin
Posts: 2625 Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Post
by John » Mon Jul 21, 2008 4:55 pm
You need the minus sign in the string, so '-10 days' means 10 days ago. Just '10 days' without the minus sign will not be recognized.
John Turnbull
Thunderstone Software
mark
Site Admin
Posts: 5519 Joined: Tue Apr 25, 2000 6:56 pm
Post
by mark » Mon Jul 21, 2008 4:56 pm
Not quite. Look closely at my example. You had
'10 days'
I have
'-10 days'
You're comparing the id to a specific point in time and "10 days" is ambiguous. "-10 days" means "10 days ago". "+10 days" would mean "10 days from now". Note that "-10 days" will be the same time of day as "now" but 10 days earlier. If you want the beginning of the day use
'begin of -10 days'