Mysql data query with x Days to expire

Asked

Viewed 722 times

2

I don’t know what I’m doing wrong. in this code below was to show me the list of records with the dates that entered the 3 days house to win only that there are coming records that have already won

       $cmd = "SELECT f.*,a.* FROM a_finan AS f
       INNER JOIN agenda_saidas AS a
       ON a.id_saida = f.id_saida
       where a.id_transfer1 = '$id_transfer'  AND f.id_transfer =     
      '$id_transfer' AND f.status_servico = 'pend'
       **AND a.start <= DATE_ADD(now(),  INTERVAL 3 DAY)**         
       ";   

exe:hoje é **06-06-2015**
Tenho registros de 04-06-2015
                   05-06-2015
                   06-06-2015
                   07-06-2015
                   08-06-2015
                   09-06-2015 
a Consulta teria que me mostar só os dias 06 , 07 e 08  

Currently it is returned to me until due dates 04 and 05. How could I fix this?

1 answer

2

Use the Between clause in your query.

SELECT f.*,a.*
FROM a_finan AS f
INNER JOIN agenda_saidas AS a
ON a.id_saida = f.id_saida
WHERE a.id_transfer1 = '$id_transfer'
AND f.id_transfer = '$id_transfer'
AND f.status_servico = 'pend'
AND a.start BETWEEN NOW() AND DATE_ADD(NOW(),  INTERVAL 3 DAY);

Or

SELECT * 
FROM a_finan AS f
INNER JOIN agenda_saidas AS a USING(id_saida)
WHERE a.id_transfer1 = '$id_transfer'
AND f.id_transfer = '$id_transfer'
AND f.status_servico = 'pend'
AND a.start BETWEEN NOW() AND DATE_ADD(NOW(),  INTERVAL 3 DAY);

If the a. id_tranfer1 or f. id_transfer is a relationship between foreign keys so I do not see the need to put WHERE a.id_transfer1 and AND f.id_transfer, just relate only one of the fields.

You may have conflict at the time of searching the dates if the database field is only date and not datetime, in this case just use the mysql DATE function. DATE(NOW())...

Browser other questions tagged

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