ewhitt:$ST=$DBH->prepare(<<EOSQL); if not exists (select * from tbl where timestamp > ?) if exists (select * from tbl where hostname=?) update tbl set timestamp=? where hostname=? else insert tbl (hostname, timestamp) values (?, ?) EOSQL $ST->execute($timestamp, $hostname, $timestamp, $hostname, $hostname, +$timestamp);
Let SQL do some of the work for you:
$ST=$DBH->prepare(<<EOSQL); if not exists (select * from tbl where hostname=?) insert tbl (hostname, timestamp) values (?, ?) else if not exists (select * from tbl where hostname=? and timestamp>?) update tbl set timestamp=? where hostname=? EOSQL $ST->execute($hostname, $hostname, $timestamp, $hostname, $timestamp, $timestamp, $hostname);
Note: I would actually use a stored procedure so I wouldn't have to repeat the $hostname and $timestamp variables in an odd order. But I'm not a MySQL user, so I thought I'd do it with basic SQL.
This way, there's only one transaction with the database for each update: You just give the database a hostname and timestamp with a couple of decisions, and it'll do the rest.
Usual disclaimers apply: Untested code, et cetera.
...roboticusIn reply to Re: Selecting most recent MySQL timestamp
by roboticus
in thread Selecting most recent MySQL timestamp
by ewhitt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |