Get records created in a MySQL date range

0

Dear friends,

I have a mysql table that receives the effective data from the workforce on a client of my company.

Each record of the DB has a start date (When this service started to be rendered on that client) and an end date (When this service is no longer rendered.

I need to get the active assets on the client between a x date and a and date.

So long, my SQL resolves.

"SELECT * FROM 'EFETIVO_POSTO' WHERE 'COD' = '$chave' AND 'INICIO' >= '$dataIni' AND 'FINAL' <= '$dataFin'"

The problem is that if this query is performed with today's date, it needs to show the effective amount that exists on that client today, regardless of the date that service started to be rendered.

Ideas are welcome not only in MySQL but also in PHP and even in JS (since this will later generate a JSON to be used by JS)

    
asked by anonymous 04.02.2016 / 12:57

1 answer

2
SELECT * FROM EFETIVO_POSTO WHERE COD = $chave AND INICIO <= CURDATE() AND FINAL IS NULL --retorna os efetivos que AINDA estão sendo executados

I imagine if it has not yet been completed, the end date should be null. So the IS NULL

Combining with the code you have already posted we have:

SELECT * FROM EFETIVO_POSTO WHERE COD = '$chave' AND ((INICIO >= '$dataIni' AND FINAL <= '$dataFin') OR (INICIO <= CURDATE() AND FINAL IS NULL)) 
    
04.02.2016 / 13:16