Мне нужно, чтобы простой запрос MySQL был преобразован в Codeigniter в виде таблиц данных.
Вот фактический запрос.
SELECT distouter.*,
SUM(purinrout.purchase_ammount) AS prevmonth
FROM (SELECT donr.*,
SUM(purinr.purchase_ammount) AS curmonth
FROM district_owner AS donr
left join district AS dist
ON donr.district_id = dist.id
left join franchise AS franinr
ON franinr.district_id = donr.district_id
left join purchase AS purinr
ON purinr.f_id = franinr.id
WHERE donr.district_id = '1'
AND purinr.purchase_date BETWEEN
Date_format(Curdate(), '%Y-%m-01') AND
Curdate()) AS distouter
left join district AS distout
ON distouter.district_id = distout.id
left join franchise AS franinrouter
ON franinrouter.district_id = distouter.district_id
left join purchase AS purinrout
ON purinrout.f_id = franinrouter.id
WHERE distouter.district_id = '1'
AND Month(purinrout.purchase_date) = Month(Date_sub(Curdate(),
interval 1 month))
Вот что я сделал
$this->CI()->datatables->select('district_owner.*');
$this->CI()->datatables->select('SUM(purchase.purchase_ammount) AS curmonth');
$this->CI()->datatables->from('district_owner');
$this->CI()->datatables->join('district', 'district_owner.district_id = district.id','left');
$this->CI()->datatables->join('franchise', 'franchise.district_id = district_owner.district_id','left');
$this->CI()->datatables->join('purchase', 'purchase.f_id = franchise.id','left');
//$this->CI()->db->where('district_owner.deleted_on', '0000-00-00 00:00:00');
$this->CI()->datatables->where('district_owner.district_id', '1');
$this->CI()->datatables->where('purchase.purchase_date BETWEEN
Date_format(Curdate(), "%Y-%m-01") AND
Curdate()');
$sub_query = $this->CI()->datatables->generate('json', 'UTF-8');
Я создал sub_query, как вы можете видеть выше, а затем передал его в основном запросе, как показано ниже
$this->CI()->datatables->select('distouter.*');
$this->CI()->datatables->select('SUM(purinrout.purchase_ammount) AS prevmonth');
$this->CI()->datatables->from('($sub_query) AS distouter');
$this->CI()->datatables->join('district', 'distouter.district_id = district.id ','left');
$this->CI()->datatables->join('franchise', 'franchise.district_id = distouter.district_id','left');
$this->CI()->datatables->join('purchase', 'purchase.f_id = franchise.id','left');
//$this->CI()->db->where('district_owner.deleted_on', '0000-00-00 00:00:00');
$this->CI()->datatables->where('distouter.district_id', '1');
$this->CI()->datatables->where('Month(purchase.purchase_date)','Month(Date_sub(Curdate(),interval 1 month)');
return $query = $this->CI()->datatables->generate('json', 'UTF-8');
Но он не работает. Пожалуйста, сообщите мне решение. Данные не являются обязательными. Так что, если вы можете предложить мне ответить, не используя таблицы данных, это тоже сработает.
Спасибо, Махеш