Sou novo aqui no fórum e gostaria de uma ajuda com um probleminha que já me consumiu algumas horas. Acredito que pode ser algum detalhe que não estou conseguindo visualizar..
Problema: Basicamente preciso de algumas informações pra montar um relatório, no MySQL Workbench consigo exatamente o que quero, porém na hora de "traduzir" a query para o códio não consigo o mesmo resultado (na vdd, não me retorna nada). A linguagem de programação é PHP no CodeIgniter.
No Workbench:
select tec_payments.sale_id, tec_payments.paid_by as pagamento,
tec_payments.date, tec_payments.amount, tec_sale_items.product_id,
tec_sale_items.category_id
from tec_payments
left join tec_sale_items on tec_sale_items.sale_id = tec_payments.sale_id
where tec_payments.date between '2019-07-03 00:00:00' and '2019-07-03 00:00:00'
and tec_sale_items.category_id = 14
and tec_payments.paid_by = 'cash';
Resultado da Consulta:
sale_id | pagamento | date | amount | product_id | category_id
1 cash 2019-07-03 00:00:00 | 18.00 83 14
1 cash 2019-07-03 00:00:00 | 18.00 83 14
2 cash 2019-07-03 00:00:00 | 145.00 94 14
2 cash 2019-07-03 00:00:00 | 145.00 95 14
6 cash 2019-07-03 00:00:00 | 37.00 66 14
6 cash 2019-07-03 00:00:00 | 37.00 43 14
7 cash 2019-07-03 00:00:00 | 17.00 76 14
No método preciso passar alguns parâmetros, mas o meu model está recebendo tudo certo. Enfim, é isso! Desde já, muito obrigado galera!
Como me foi sugerido, aqui está o código em PHP (Esse é o model):
public function getItensDate($categoria = null, $start_date = null, $end_date = null) {
$pagamento = 'cash';
$whereCategoria = "";
$whereData = "";
$wherePagamento = "AND payments.paid_by = " . $pagamento;
if ($start_date != null) {
$whereData = "WHERE payments.date BETWEEN " . $this->db->escape($start_date) . " AND " . $this->db->escape($end_date);
}
if ($categoria != null) {
$whereCategoria = "AND sale_items.category_id = " . $categoria;
}
$query = "SELECT payments.sale_id, payments.paid_by as pagamento, payments.date, payments.amount, sale_items.product_id, sale_items.category_id
FROM payments
LEFT JOIN sale_items ON sale_items.sale_id = payments.sale_id
$whereData $whereCategoria $wherePagamento";
//print_r($query);
//exit;
return $this->db->query($query)->result();
}