mysql - DBIx::Class: Only select results where has_many greater than zero -
mysql - DBIx::Class: Only select results where has_many greater than zero -
in our mysql database, have third_party_accounts
table , has_many
third_party_campaigns
. however, not accounts have campaigns. in dbix::class
select accounts have 1 or more campaigns. simplest i've found follows:
my $third_party_account_rs = $schema->resultset('thirdpartyaccount'); $with_campaigns_rs = $third_party_account_rs->search( { third_party_account_id => \'is not null' }, { bring together => 'third_party_campaigns', group_by => 'me.id', } );
for relevant database columns:
mysql> select id third_party_accounts; +----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec) mysql> select id, third_party_account_id third_party_campaigns; +----+------------------------+ | id | third_party_account_id | +----+------------------------+ | 1 | 1 | | 2 | 2 | | 3 | 1 | +----+------------------------+ 3 rows in set (0.00 sec)
this seems such obvious utilize case i'm sure there's simple way this, can't find it.
my $with_campaigns_rs = $schema->resultset('thirdpartycampaigns') ->search_related('third_party_account');
mysql perl orm dbix-class
Comments
Post a Comment