fluentPDO中的子查询

时间:2015-11-30 21:41:52

标签: php mysql 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

任何帮助?

1 个答案:

答案 0 :(得分: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将推断两个表之间的关系。