2
I have a table where I record the time when a particular request had its status_id
changed. This table I call historico_status_solicitacoes
.
In this table, I have the following fields: id
, status_id
, solicitacao_id
, usuario_id
, created_at
.
For each request that has the status changed, a record is entered.
Thus:
+----+-----------+----------------+------------+---------------------+
| id | status_id | solicitacao_id | usuario_id | created_at |
+----+-----------+----------------+------------+---------------------+
| 1 | 1 | 1 | 1 | 2018-10-29 17:28:46 |
| 2 | 2 | 1 | 1 | 2018-10-29 17:38:50 |
| 3 | 3 | 1 | 1 | 2018-10-29 17:48:16 |
| 4 | 4 | 1 | 1 | 2018-10-29 18:58:46 |
| 5 | 5 | 1 | 1 | 2018-10-29 19:31:46 |
| 6 | 6 | 1 | 1 | 2018-10-30 10:20:00 |
+----+-----------+----------------+------------+---------------------+
What do I need to do? I need to do a query, where I can compute the difference in minutes between a line and another, considering the shorter date for the longer date (which in this case may be the id
also, since it is created sequentially, and the highest value is always the latest).
I did something similar to calculate the current balance using the previous line, but I have no idea how to work with date difference in MYSQL.
Someone can help out?
Geez, there’s not 10 seconds I posted the question and there’s already a negative? What’s going on? Could you explain why?
– Wallace Maxters
I wasn’t the one who said no, but why do it in the bank? You can’t go straight to the front?
– Costamilam
@Guilhermecostamilam if there are too many returns, I might want to resort to a pagination. And in a pagination, you wouldn’t have the previous value if you were on the second page. I even thought of asking the question in the sense of taking it to the side of Laravel, but in this case, I prefer in the same database.
– Wallace Maxters
But you have to think about the performance issue as well, this can be very important for the bank, something that would be simple on the front end. ps.: but I was not the one who negative xD
– aa_sp
@aa_sp honestly, depending on the size of the pagination, it would make up more to do the calculation in the bank, wouldn’t it? Why a status could be repeated in the history. Of course it is a rare case, but let’s assume that there is a case of me having 1000 records. It’s good to think on this side, because the goal of the site is to have a more canonical response that can help other users.
– Wallace Maxters
@Wallacemaxters need to evaluate, depending on the times a simple comparison variable would solve. But Voce, who knows the system, needs to evaluate and test the options, thinking not only how the base is now, but in the future.
– aa_sp
@Wallacemaxters, wouldn’t it be interesting to create a column
diferenca
and at the time of inserting calculate and inform the difference for the last record?– Roberto de Campos
@Robertodecampos is against database normalization, isn’t it? Calculating the data is more likely to have the right value than a column that can be erroneously changed.
– Wallace Maxters