MySQL: Update on Select - Timestamp Example

There comes a time when it is appropriate to update a MySQL table when only a SELECT statement is being executed. You may be tracking the number of reads, or marking a column with the latest TIMESTAMP.

Unfortunately, there is no 'UPDATE ON SELECT' property available in MySQL to do this automatically. Instead, one must rely on executing a compound statement. In my case, my table holds proxy usage information, and it is useful to keep track of when these proxies are used. To do this, the statement I use to get a proxy is this:

SELECT proxy FROM proxy_usage;UPDATE proxy_usage SET timestamp=NOW();

There are more complicated ways to do this, but the basis is simple. You must insert an extra command into your query, and the simplest way is to separate them with a semicolon. And no, you cannot set up a trigger for select statements.

No comments:

Post a Comment