Friday, August 22, 2008

Timeout using SQL with Oracle

Yesterday I showed you how it was possible to "timeout" or make something disappear from your webpage after a certain number of days using PHP. I discussed briefly why that option was not necessarily a practical choice in that given situation. The better way to get the same result is to never have pulled the data from the database to begin with.

In Oracle you can easily perform arithmetic on dates in your SELECT statement to limit your results based on how old the entry is. For example, If I was going to pull out the usernames, messages, and dates from a database in which the posted date was within the last 7 days I could easily do that with a SQL SELECT statement.

In this example, POST_TABLE is my database table, and POST_DATE is the date of the post. I can use a SELECT statement similar to the one below to get all the messages within the last 7 days and order the results by the post date.

SELECT USERNAME, MESSAGE, POST_DATE FROM POST_TABLE WHERE (SYSDATE - POST_DATE < 7) ORDER BY POST_DATE

As you can see, it is a really simple solution that is much more elegant then the PHP based solution I discussed yesterday. Either way there are certain situations in which both solutions have their place. If you have any questions, comments, suggestions, or even complaints, leave them in the comments.

Shane

No comments: