0
這是在選擇子查詢和連接子查詢。查詢如下。如何在codeigniter活動記錄中編寫sql子查詢?
SELECT t1.BranchId,t1.BranchName,FORMAT(TotalRevenue/total_trans,2) AS Average,t1.ReadingDate
from (SELECT br.BranchName,
count(nooftrans)as total_trans,zf.BranchId,ReadingDate,zf.zread_id
FROM `ospos_zreads_pdf` zf , ospos_branches br,ospos_zreads_pdf_activity_status zpf
WHERE br.BranchId = zf.BranchId and zf.zread_id=zpf.zread_id and ReadingDate >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) and zpf.zreadactivity='Sales Transaction'
group by zf.BranchId)t1
join (SELECT br.BranchName,sum(TotalRevenue)as TotalRevenue,zf.BranchId,zread_id,ReadingDate
FROM `ospos_zreads_pdf` zf , ospos_branches br
WHERE br.BranchId = zf.BranchId and ReadingDate >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
group by zf.BranchId)t2 on t1.zread_id=t2.zread_id
閱讀[這](HTTP://codeblog.jonskeet .uk/2010/08/29/writing-the-perfect-question /)被認爲是如何提出問題的。 – Tpojka