my $query = "SELECT DISTINCT ID,unix_timestamp(tUpdate) as t,unix_timestamp(tEnter) as p from ini_Rate where tUpdate > tEnter limit 5 "; $sth = $dbh->prepare($query); $sth->execute(); while ( my $row = $sth->fetchrow_hashref) { my $cRequest = ""; my $cTratupdated = ""; if($row->{t} > $row->{p}) { { $cRequest = "U"; # Update $ctupdate = $row->{tUpdate}; } else { $cRequest = "N"; # New }