sum specific line in Mysql

Asked

Viewed 275 times

0

Speech person!

I am doing the insertion of a time (hour:minutes:seconds) in a column x, I wonder if it is possible to add the current value of the line with the next value that is inserted? like it’s a += . For example: An Idx row from the Time Column, it gets a value of 01:00:00 and then gets the value of 20 minutes, that is, 01:20:00. do this without the need to query the line and add with the new value and then update. Thank you.

The table is as follows:

# nomeID       situação      tempo    tTotal

127.0.0.1       Disponi    00:00:10  00:00:00
192.168.1.102   Ocupada    00:00:00  00:00:00
192.168.1.103   Disponi    01:26:00  01:26:00
192.168.1.104   Ocupada    00:00:00  00:00:00    

1 answer

1


Let’s say the new value to be added is 20 min -> 00:20:00 and the id is 1, then it would look like this:

UPDATE tabela
SET tTempo = SEC_TO_TIME( TIME_TO_SEC(tTempo) + TIME_TO_SEC('00:20:00') )
WHERE id = '1'
  • The point is that the time 01:00:00 will already be in my database, what I would like to do is that the next time another time is inserted it will make the sum with what is already in the bank.

  • Okay. In the table already has a row that time is 01:00:00. Then you will insert another line with 00:20:00, but you do not want this new line to stay with 00:20:00 and yes, that it adds with the previous one, getting this new line with 01:20:00. That’s it?

  • Exactly that!

  • I edited the answer, see if it fits.

  • Almost that, only he’s creating a new line without the ID and with the added value

  • Just put in the INSERT all the fields you populate in the table. See how you got two more fields.

  • But this way I have to insert new values to these fields? or I can modify only the time field?

  • If you are going to insert a new line, the correct is to feed all the necessary fields. For what you are talking about then you will not do an INSERT but an UPDATE.

  • Exactly, the idea is to add the value of the field to the new value, in the same row in the ttotal column. adding the value time to the value already in ttotal

  • Blz @Leoneazevedo, I modified sql again, now for an UPDATE.

  • Hello, I did it differently but it was useful for me to get the result. Thank you.

Show 6 more comments

Browser other questions tagged

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