2012-01-20 2 views
3

MySQL 데이터베이스에서 third_party_accounts 테이블이 있으며 has_manythird_party_campaigns입니다. 그러나 모든 계정에 캠페인이있는 것은 아닙니다. DBIx::Class에서 내가하고 싶은 것은 하나 이상의 캠페인이있는 계정 만 선택하는 것입니다. 관련 데이터베이스 컬럼에 대한DBIx :: Class : 오직 0보다 큰 has_many가있는 결과 만 선택하십시오.

my $third_party_account_rs = $schema->resultset('ThirdPartyAccount'); 
my $with_campaigns_rs  = $third_party_account_rs->search(
    { third_party_account_id => \'IS NOT NULL' }, 
    { 
     join  => 'third_party_campaigns', 
     group_by => 'me.id',                                 
    } 
); 

을 : 다음과 같이 내가 찾은 된 간단한이다

이 내가 할 수있는 간단한 방법이있을거야 그런 뻔한 사용 사례처럼 보인다
mysql> select id from third_party_accounts; 
+----+ 
| id | 
+----+ 
| 1 | 
| 2 | 
| 3 | 
+----+ 
3 rows in set (0.00 sec) 

mysql> select id, third_party_account_id from third_party_campaigns; 
+----+------------------------+ 
| id | third_party_account_id | 
+----+------------------------+ 
| 1 |      1 | 
| 2 |      2 | 
| 3 |      1 | 
+----+------------------------+ 
3 rows in set (0.00 sec) 

이,하지만 그것을 찾을 수 없습니다.

답변

5
my $with_campaigns_rs = 
    $schema->resultset('ThirdPartyCampaigns') 
    ->search_related('third_party_account'); 
+1

이것은 이미 제안되었지만 작동하지 않습니다. 원래 쿼리에서 나는'group_by => "me.id"'를 가지고 있었다. 그건'ThirdPartyAccount' 레코드가 중복되는 것을 막아줍니다. 위의 데이터를 가지고있는이 "역"솔루션은 ID가 1 인 두 개의'ThirdPartyAccount' 객체를 남겨 둘 것입니다. – Ovid

+1

중복 반환을 피하기 위해'-> search_related '에'{distinct => 1}'을 추가하십시오 '전화. – ilmari

관련 문제