这是在选择子查询和联接子查询中。查询如下。

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

最佳答案

您可以使用第二个参数来禁用某些数据库方法的保护:

$this->db->select('id, title, (SELECT COUNT(*) FROM other) as count', FALSE)


文档中的更多内容:
http://www.codeigniter.com/user_guide/database/query_builder.html

关于mysql - 如何在Codeigniter事件记录中编写sql子查询?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32630497/

10-13 09:16