../cgi-bin/script.pl?column3=1&column2=2&column1=3 #### my $q = new CGI; my $href = $q->Vars; #### my $col_string = join( ',', sort {$$href{$a} <=> $$href{$b}} keys %$href ); #### $col_string =~ s/^,+//; # remove leading commas $col_string =~ s/,+$//; # remove trailing commas $col_string =~ s/,{2,}/,/g; # reduce adjacent commas to "," #### my $col_string = ''; for my $var ( sort {$$href{$a} <=> $$href{$b}} keys %$href ) { $col_string .= "$var,"; } chop $col_string; # remove final comma #### my $sql = "SELECT $col_string FROM my_table";