Select distinct

Selecting Distinct Values

Problem:

You want the Class::DBI equivalent of "SELECT DISTINCT column FROM table;" For example, "SELECT DISTINCT address FROM photos WHERE projno=?"

Solution:

There isn't really an equivalent, as Class::DBI is about returning objects rather than values.

In this sort of case, people generally use Perl:

my %seen; my @address = grep !$seen{$_}++, map $_->address, Photo->search(projno => $value);

But you can also merrily write your own method to do this:

__PACKAGE__->set_sql(distinct_col_by_proj => <<''); SELECT DISTINCT(%s) FROM  __TABLE__ WHERE projno = ? sub addresses_for_proj { my ($class, $projno) = @_; return $class->sql_distinct_col_by_proj('address')->select_col($projno); }

Solution by Tony via list

Transcribed by Vince Veselosky