Retrieve records created in a Mysql date range

Asked

Viewed 118 times

0

Dear friends,

I have a mysql table that receives manpower effective data in a client of my company.

Each DB record has a start date (When this service began to be provided in this client) and a end date (When this service ceased to be provided.

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

Until then, my SQL solves.

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

The problem is that if this consultation is carried out with today’s date, it needs to show the effectiveness that exists in that customer today, regardless of the date when this service began to be provided.

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)

  • 1

    Do you want the personnel that are running today? That is, they started today or at some earlier date and are still running?

  • @Pedro Teles Isso ai, from earlier dates that are still running.

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 that if it has not yet been completed, the final date should be null. Hence the instruction IS NULL

Matching the code you’ve already posted we have:

SELECT * FROM EFETIVO_POSTO WHERE COD = '$chave' AND ((INICIO >= '$dataIni' AND FINAL <= '$dataFin') OR (INICIO <= CURDATE() AND FINAL IS NULL)) 
  • That should do it to catch the ones that are currently running. Then I do two different situations, one picking up those who were active in a certain time range and this to catch those of today?

  • If you need the two situations separately yes. But if you need to list at the same time you will have to combine the two selects

  • The ideal would be to list at the same time. And this combination of the two selects would be done as? Joining the resulting arrays with php?

  • Take a look at the edition I made and see if it fits you.

  • Perfect! Thanks to Peter.

Browser other questions tagged

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