如何在CodeIgniter中查找具有不同where条件的特定行和其他列的计数

时间:2019-02-25 03:12:47

标签: mysql codeigniter

这是我的模型代码:

function getSaleOrderFormInfoDetail($saleOrderId)
{
$this->db->_protect_identifiers=false;

$this->db->select('BaseTbl.itemId, count(CASE WHEN SaleOrder.orderDate >= DATE_ADD(curdate(), INTERVAL -6 MONTH) AND SaleOrder.orderDate <= curdate() THEN SaleOrder.orderDate ELSE NULL END) as totalOrderLast6Month');
$this->db->from('tbl_saleorderdetail as BaseTbl');
$this->db->join('tbl_saleorders as SaleOrder', 'SaleOrder.saleOrderId = BaseTbl.masterId','left');
$this->db->where('BaseTbl.masterId', $saleOrderId);
$this->db->order_by("BaseTbl.id", "asc");
$query = $this->db->get();

return $query->result();
}

从该查询中,我获得了销售订单的每个项目,其最近6个月的销售记录计数带有固定的where子句$this->db->where('BaseTbl.masterId', $saleOrderId);。 我想获取此where子句的项目,但我还需要最近6个月all个销售订单的记录计数。

1 个答案:

答案 0 :(得分:0)

尝试两次致电getSaleOrderFormInfoDetail

第一次有条件,第二次没有条件

function getSaleOrderFormInfoDetail($saleOrderId = '')
{
$this->db->_protect_identifiers=false;

$this->db->select('BaseTbl.itemId, count(CASE WHEN SaleOrder.orderDate >= DATE_ADD(curdate(), INTERVAL -6 MONTH) AND SaleOrder.orderDate <= curdate() THEN SaleOrder.orderDate ELSE NULL END) as totalOrderLast6Month');
$this->db->from('tbl_saleorderdetail as BaseTbl');
$this->db->join('tbl_saleorders as SaleOrder', 'SaleOrder.saleOrderId = BaseTbl.masterId','left');
if(!empty($saleOrderId)){
   $this->db->where('BaseTbl.masterId', $saleOrderId);
}
$this->db->order_by("BaseTbl.id", "asc");
$query = $this->db->get();

return $query->result();
}

现在就这样

$counter = count($this->model_name->getSaleOrderFormInfoDetail());

这将计算行数

$data = $this->model_name->getSaleOrderFormInfoDetail($saleOrderId);

这将为您提供条件数据