# get the first relationship my $firstrel = shift @{$refs->{related}}; # compose some sql my $sql = qq{ SELECT * FROM $refs->{table} AS $refs->{alias} JOIN $firstrel->{name} AS $firstrel->{alias} ON } . join (' AND ', @{$firstrel->{joins}}); print $sql; __END__ output: SELECT * FROM this_table AS tt JOIN other_table AS ot ON tt.person_id = ot.person_id AND tt.week_id = ot.week_id