$sth=$dbh->prepare("select data from mytable"); $sth->execute; while ($data = $sth->fetchrow_arrayref()) { if $data->[0],$data->[1],$data->[2] is the same as the equivalent elements from previous record then print $data->[4] otherwise print complete $data record } $sth->finish; $dbh->disconnect;