如何在codeigniter活动记录中编写sql子查询?

时间:2015-09-17 12:38:29

标签: mysql codeigniter

这是在select子查询和join子查询中。查询如下。

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 

1 个答案:

答案 0 :(得分:0)

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

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

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