$sth->bind_columns( undef, \$orig_service_id, \$orig_stop, $depart_orig); while ($sth->fetch()) { $ids{$orig_service_id} = { stop_reference => $orig_stop, depart_time => $depart_orig, } } #### $sth->bind_columns( undef, \$dest_service_id, \$dest_stop, $depart_dest); while ($sth->fetch()) { if (exists ($ids{$dest_service_id}) { print "ID $id exists in both origin and destination set\n"; print "\t$ids{$dest_service_id}{stop_reference} => $dest_stop\n"; print "\t$ids{$dest_service_id}{depart_time} => $depart_dest\n"; } }