Help with a mysql query

Asked

Viewed 61 times

1

personal to breaking the head here with a query I assembled where I pick up the dates informed in the form and search in the database.

it makes the query only that it does not bring all the data

If I inform the initial date of 01/01/2017 until today 15/11/17 it only brings until the day 13/10/2017.

if I inform only the initial date it brings all the information, if I inform only the final date it brings nothing, then my problem is at the end date <=

how do I fix it?

my select is like this:

SELECT c.dia, c.mes, c.ano, c.id id_pedido, c.total total_pedido, c.tipo, d.cat_id id_prod,
SUM( d.valor_total ) total_prod
FROM lc_controle c
INNER JOIN lc_detalhe d ON d.controle_id = c.id
WHERE c.tipo =  '0'  and
        ano >= '$anoIni' AND
        mes >= '$mesIni' AND
        dia >= '$diaIni' AND
        ano <= '$anoFim' AND
        mes <= '$mesFim' AND
        dia <= '$diaFim'
    GROUP BY
        c.id,
        c.total,
        d.cat_id

1 answer

2

Correct is you use the BETWEEN:

$data_inicial = "2017-01-01";
$data_final = "2018-01-01";

SELECT c.dia, c.mes, c.ano, c.id id_pedido, c.total total_pedido, c.tipo, d.cat_id id_prod,
SUM( d.valor_total ) total_prod
FROM lc_controle c
INNER JOIN lc_detalhe d ON d.controle_id = c.id
WHERE data BETWEEN '".$data_inicial."' AND '".$data_final."'
AND WHERE c.tipo =  '0' 
    GROUP BY
        c.id,
        c.total,
        d.cat_id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.