#php #codeigniter #codeigniter-3 #codeigniter-2
#php #codeigniter #codeigniter-3 #codeigniter-2
Вопрос:
Это мой запрос в модели:
return $this->db->join('tbl_customer', 'tbl_customer.cus_code = tbl_cus_account.custcode')
->where("status", 1)
->where("DATE_FORMAT(nextbillingdate,'%Y-%m') <= ", date('Y-m'))
->select('*,MAX(issuewithmain) AS issuewithmain, SUM(monthlyfee) AS monthlyfee, count(accountcode) as rows')
->group_by('custcode')
->get('tbl_cus_account');
Комментарии:
1. какой результат отображается после выполнения вашего запроса модели?
2. привет, Навин! Результат всегда отображается для первой строки, это означает, что он отображается для строки accountcode 4
3.
tbl_customer
Будет иметь несколькоtbl_cus_account
?4. Да, у tbl_customer много учетных записей tbl_cus_account. Это означает один ко многим
Ответ №1:
Ваш запрос может быть примерно таким
SELECT t.*, t2.rows, t2.monthlyfee from tbl_cus_account t join (SELECT MAX(issuewithmain) AS issuewithmain, custcode, count(accountcode) as rows, SUM(monthlyfee) AS monthlyfee from tbl_cus_account JOIN tbl_customer ON tbl_customer.cus_code = tbl_cus_account.custcode where status = 1 AND DATE_FORMAT(nextbillingdate,"%Y-%m") <= DATE_FORMAT(now(),"%Y-%m") GROUP BY custcode) t2 on t.issuewithmain = t2.issuewithmain and t.custcode = t2.custcode
Здесь вы выполнили дополнительное объединение с той же таблицей, чтобы получить только те записи, которые соответствуют вашему max(issuewithmain)
Вам нужно преобразовать этот запрос в codeigniter и добавить условия where в соответствующую таблицу.
Может быть, что-то вроде этого, я это не тестировал
Обновить
return $this->db->join('(SELECT MAX(issuewithmain) AS issuewithmain, custcode, count(accountcode) as rows, SUM(monthlyfee) AS monthlyfee from tbl_cus_account where status = 1 AND DATE_FORMAT(nextbillingdate,"%Y-%m") <= "'.date('Y-m').'" GROUP BY custcode) t2', 'tbl_cus_account.issuewithmain = t2.issuewithmain AND t2.custcode = tbl_cus_account.custcode')->select('tbl_cus_account.*, t2.monthlyfee, t2.rows') ->join('tbl_customer', 'tbl_customer.cus_code = t2.custcode') ->group_by('t2.custcode') ->get('tbl_cus_account');
Комментарии:
1. ошибка с таблицей объединения клиента с tbl_cus_account -> join(‘tbl_customer’, ‘tbl_customer.cus_code = tbl_cus_account.custcode’), и сумма ежемесячного платежа не соответствует сумме
2. Я обновил свой анализатор, теперь соединение с tbl_customer находится внутри нового соединения. Ошибка с объединением tbl_customer должна быть из-за ошибки в имени с отношением столбцов, если вы сообщите мне правильные имена, я изменю его.
3. Nerea, это все еще ошибка, такая же, как в таблице объединения. И ключевым отношением этих двух являются tbl_customer (cus_code) и tbl_cus_account (custcode)
4. Привет, Nerea! Большое вам спасибо за вашу помощь.
5. верните $this-> db-> join(‘(ВЫБЕРИТЕ MAX (issuewithmain) КАК issuewithmain, пользовательский код, count (accountcode) как строки, SUM (monthlyfee) КАК monthlyfee из tbl_cus_account, где status = 1 И DATE_FORMAT(nextbillingdate,»%Y-%m») <= «‘.date(‘Y-m’).'» ГРУППИРУЙТЕ По пользовательскому коду ) t2′, ‘tbl_cus_account.issuewithmain = t2.issuewithmain И t2.custcode = tbl_cus_account.custcode’) //-> выбрать (‘tbl_cus_account.*, t2.monthlyfee, t2.rows’) -> присоединиться (‘tbl_customer’, ‘tbl_customer.cus_code = t2.custcode’) -> group_by(‘t2.custcode’) -> получить (‘tbl_cus_account’);