Selecting all rows where id is within n days

mjacobson
Posts: 204
Joined: Fri Feb 08, 2002 3:35 pm

Selecting all rows where id is within n days

Post by mjacobson »

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?
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Selecting all rows where id is within n days

Post by mark »

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

Selecting all rows where id is within n days

Post by mjacobson »

So my select is OK, I just need to put an index on it.
User avatar
John
Site Admin
Posts: 2625
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH

Selecting all rows where id is within n days

Post by John »

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
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Selecting all rows where id is within n days

Post by mark »

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'