#!perl use strict; use DBI; use Data::Dumper; my $dbh = create_db(); my $sql = 'SELECT P.common_name,S.plant_size,S.unit,G.abbr FROM plants as P LEFT JOIN sizes as S ON S.plantid = P.id LEFT JOIN growers as G ON S.plantid = G.plantid AND S.plant_size = G.plant_size'; my $sth = $dbh->prepare($sql); $sth->execute(); my %hash=(); while (my @f = $sth->fetchrow_array){ $hash{$f[0]}{$f[1].$f[2]}{$f[3]} = 1 } print Dumper(\%hash);