我之前要求a question形成一个查询,以便在我的应用中获取模型的总计。它得到了有益的回答,但查询非常复杂 - 这不是我设想的雄辩适合的。 我想避免使用完全原始的查询来提高RDMS的可比性,但我在使用Laravel中的查询构建器方面没有多少经验。我如何在Laravel中形成以下内容?:
SELECT payers_payments.payer_id,
Sum(payers_payments.amount) AS total_paid,
Sum(payers_payments.pays * payments_share.single_share) AS fair_share
FROM payers
INNER JOIN (payers_payments
INNER JOIN (SELECT payment_id,
Sum(amount) / Sum(pays) AS single_share
FROM payers_payments
GROUP BY payment_id) AS payments_share
ON payers_payments.payment_id =
payments_share.payment_id)
ON payers.id = payers_payments.payer_id
WHERE (( ( payers.user_id ) = 1 ))
GROUP BY payers_payments.payer_id;
注意事项:
payers
表,因为我希望将结果限制为某个user_id
。因此,有一个额外的连接。payments_share
是在查询期间用于聚合结果的临时表(我认为)。查询构建器可以处理这个吗?