gpt4 book ai didi

php - FluentPDO 中的子查询

转载 作者:行者123 更新时间:2023-11-29 21:47:16 24 4
gpt4 key购买 nike

我正在尝试将此查询“翻译”为流畅的PDO 语句,但我找不到使用子查询的方法

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

有什么帮助吗?

最佳答案

我不知道还有比将原始 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 将通过哪种方式推断两个表之间的关系。

关于php - FluentPDO 中的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34008550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com