It looks likely your table structure is not ideal but if you are stuck with it you can use user defined variables to hold previous record values
SELECT key_number, dateadded, IF (action=@last_action AND key_number=@last_key,"ERROR","OK"), @last_action := action, @last_key := key_number FROM ( SELECT * FROM keysafe ORDER BY key_number, dateadded ) as x
Initialise the variables before use with
$dbh->do('SET @last_action := "UNKNOWN",@last_key="UNKNOWN"');
alternatively use perl variables
pojmy $sql = 'SELECT key_number,action,dateadded FROM keysafe ORDER BY key_number, dateadded'; my $sth = $dbh->prepare($sql); $sth->execute(); my ($last_key,$last_action); while (my ($key,$action,$date) = $sth->fetchrow_array){ my $status = 'OK'; if (($key eq $last_key) && ($action eq $last_action)){ $status = 'ERROR'; }; print "$key,$action,$date,$status\n"; $last_key = $key; $last_action = $action; }
In reply to Re: Searching Database Question
by poj
in thread Searching Database Question
by htmanning
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |