2015-11-30 39 views
0

我想「翻譯」這個查詢到fluentPDO聲明,但我不能找到一種方法,使用子查詢它子查詢中fluentPDO

SELECT plan.*, COUNT(DISTINCT click.id) AS clicks, COUNT(DISTINCT impression.id) as impressions, IFNULL(A.earnings, 0) AS earnings FROM plan 
LEFT JOIN click ON click.plan_id = plan.id 
LEFT JOIN impression ON impression.plan_id = plan.id 
LEFT JOIN (
    SELECT plan_id AS earnings_id, SUM(signup.earning) as earnings FROM signup 
) A ON A.earnings_id = plan.id 
GROUP BY plan.id 

任何幫助嗎?

回答

0

我不知道的不僅僅是將自己的原始SQL子查詢的leftJoin方法的更好的方式:

$fluent->from('plan') 
->select('COUNT(DISTINCT click.id) AS clicks, COUNT(DISTINCT impression.id) as impressions, IFNULL(A.earnings, 0) AS earnings') 
->leftJoin('click ON click.plan_id = plan.id') 
->leftJoin('impression ON impression.plan_id = plan.id') 
->leftJoin('(SELECT plan_id AS earnings_id, SUM(signup.earning) as earnings FROM signup) A ON A.earnings_id = plan.id') 
->groupBy('plan.id'); 

我發現SitePoints's FluentPDO guide非常有用的,但他們不提子查詢。他們還建議您不必明確地將加入點擊計劃和印象留在計劃中,但實際上,我永遠無法知道FluentPDO會以哪種方式推斷兩個表之間的關係。