my $statement =q{ SELECT tasks.id as tid, long_desc, employees.fname as first, employees.lname as last, DATE_FORMAT(start_date,"%b %d %y") as fdate, status.descr as status_d FROM employees,tasks,status WHERE owner = employees.id AND status = status.id }; if (exists $ARGS{status} && $ARGS{status} >0){ $statement .= " AND status = $ARGS{status}"; }elsif (exists $ARGS{status} && $ARGS{status} eq "active"){ $statement .= " AND (status=1 OR status=2)"; } if (exists $ARGS{owner} && $ARGS{owner} > 0){ $statement .= " AND owner = $ARGS{owner}"; } if ($ARGS{order} eq "osd"){ $statement .= q{ ORDER BY employees.lname, status.descr,start_date }; }elsif ($ARGS{order} eq "ods"){ $statement .= q{ ORDER BY employees.lname, start_date, status.descr }; }elsif ($ARGS{order} eq "sdo"){ $statement .= q{ ORDER BY status.descr, start_date,employees.lname }; }else{ $statement .=q{ ORDER BY start_date,employees.lname,status.descr }; } my $sth = $dbh->prepare($statement) or die $dbh->errstr; $sth->execute;