my $sth = $dbh->prepare("select object_id from mw_export_record where table_object = 'advertisers'"); $sth->execute(); @results = grep $_, map{ $_->[0] } @{$sth->fetchall_arrayref}; @results = qw/NULL/ if (@results == 0); $sth = $dbh->prepare("select object_id from mw_export_record where object_id not in (" . join(',', @results) . ")"); $sth->execute();