開始與左3與你的圖你可以像他們一樣加入他們
select *
from companies
inner join dbas ON companies.id = dbas.company_id
inner join mailers ON dbas.id = mailers.dba_id
得到「結果首先由公司分組,然後Mail_date「將GROUP BY子句添加到列出這些列的查詢中,並將這些列包括在ELECT子句中。
SELECT
companies.id, companies.name, mailers.mail_date
from companies
inner join dbas ON companies.id = dbas.company_id
inner join mailers ON dbas.id = mailers.dba_id
GROUP BY
companies.id, companies.name, mailers.mail_date
現在,當它下降到付款(例如)事情會變得棘手,因爲你想要得到那些準確的總結。如果不是所有的公司都會有訂單,你需要使用LEFT JOIN現在,並計算支付的總和,我建議你做那些資金加入到其他表,如在此之前:
SELECT
companies.id, companies.name, mailers.mail_date, SUM(p.payment_amt)
from companies
inner join dbas ON companies.id = dbas.company_id
inner join mailers ON dbas.id = mailers.dba_id
left join customer_orders ON dbas.id = customer_orders.dba_id
left join (
select customer_order_id, SUM(payment_amt) as payment_amt
from payments
group by customer_order_id
) p on customer_orders.id = p.customer_order_id
GROUP BY
companies.id, companies.name, mailers.mail_date
您有更多的邏輯包括在你的最終查詢中,但是希望它能讓你開始。
示例數據和期望的結果將真正幫助解釋你想要做什麼。 –
感謝您添加所需的resulsts。現在請添加示例數據輸入。然後請顯示嘗試做某事。 – philipxy