1
I have a table called OperacoesCaixa
and in it I have the following fields:
Cod, Valor, DataOp
Well I need to make one select
that I return the releases of the last 3 days, but I do not want to inform a date.
Assuming I have a launch held 15 days ago the select
have to return the last 3 days.
I tried to do it this way:
SELECT
*
FROM
OperacoesCaixa
WHERE
DataOp BETWEEN CURDATE() - INTERVAL 3 DAY AND CURDATE()
ORDER BY
DataOp
DESC
The problem is that it picks up the current date, if there is no release in the last 3 days the select
you won’t return anything to me.
-------- Edit --------
Good to facilitate I will post a real example.
We will use the date of 17-07-2020 as reference of the consultation day.
Example 1:
I have the following data in the table:
Cod Valor DataOp
1 85.20 13-07-2020
2 97.14 13-07-2020
3 89 13-07-2020
4 100 15-07-2020
5 247.85 15-07-2020
6 58 16-07-2020
7 97 16-07-2020
8 86 16-07-2020
9 875 17-07-2020
10 85.20 17-07-2020
Good the select
must return the following result:
10 85.20 17-07-2020
9 875 17-07-2020
8 86 16-07-2020
7 97 16-07-2020
6 58 16-07-2020
5 247.85 15-07-2020
4 100 15-07-2020
That is, the launch of the last 3 days.
Example 2 note that in the last 3 days there were no releases:
I have the following data in the table:
Cod Valor DataOp
1 85.20 5-07-2020
2 97.14 5-07-2020
3 89 5-07-2020
4 100 8-07-2020
5 247.85 8-07-2020
6 58 9-07-2020
7 97 9-07-2020
8 86 10-07-2020
9 875 10-07-2020
10 85.20 10-07-2020
The answer must be:
10 85.20 10-07-2020
9 875 10-07-2020
9 875 10-07-2020
8 86 10-07-2020
7 97 9-07-2020
6 58 9-07-2020
5 247.85 8-07-2020
4 100 8-07-2020
Summarizing independent of today’s date I want to be listed all the records of the 3 days that there was record.
Your explanation is confused. If you want to take the 3 previous releases regardless of the date they occurred then select the previous releases to the current date, order in descending order of Dataop and use a LIMIT 3.
– anonimo
Wouldn’t be the three most recent dates ?
– Motta
@Motta edited my question by putting an example to make it clearer
– Hugo Borges
Make a select distinct of the dates , order decreasing and with "limit" take the larger N , make now a select from the original table searching for IN the select above as subselect , I have only doubt in this limit or top because it is not mysql
– Motta
@Motta could post an answer with an example?
– Hugo Borges