How do I subtract and display the result in the MYSQL query

Asked

Viewed 70 times

0

I have 3 tables

Vehicles

idVeiculo | Placa | Prefixo

oleomotor

idMotor | idVeiculo | datatroca | kmtroca | horimetroca | proximatroca |idMecanico

I already have this query below that shows the result of the last changes of each vehicle without repeating the vehicle. how do I include in this query the table hodomentro that will take the last hour of each vehicle and subtract with the proximatroca and give the result of how many hours left.

SELECT DISTINCT b.prefixo as idVeiculo, max(datatroca) as datatroca, max(kmtroca) as kmtroca, max(horimetroca) as horimetroca, max(proximatroca) as proximatroca, idMecanico FROM oleomotor a
                  JOIN veiculos b on (a.idVeiculo=b.idVeiculo)
                  GROUP BY a.idVeiculo order by datatroca DESC;

Hodometre

idVeiculo | km | horimetro

1 answer

0

You can use the Function TIMEDIFF() which aims at returns the difference between two time values (expressed as a time value).

Add: timediff(proximatroca, max(horimetro)) as Horas Restantes

Would look like this:

SELECT DISTINCT b.prefixo as idVeiculo,
                max(datatroca) as datatroca,
                max(kmtroca) as kmtroca,
                max(horimetroca) as horimetroca,
                max(proximatroca) as proximatroca,
                idMecanico,
                timediff(proximatroca, max(horimetro)) as 'Horas Restantes'
FROM oleomotor a
JOIN veiculos b on (a.idVeiculo=b.idVeiculo) 
JOIN hodometro h on (a.idVeiculo= h.idVeiculo) 
GROUP BY a.idVeiculo 
ORDER BY datatroca DESC;
  • timediff not working out...

  • So the kind of field is not DATETIME correct?

  • is not same example number 60200

  • I’ll send you the other post this clearer

  • https://answall.com/questions/295453/como-fazer-a-subtract%C3%A7%C3%A3o-mysql

  • I just looked,really timediff would never work because it only works with date time.

  • In the table illustration presented by vc, the subtraction happens because the Missing field presents a number corresponding to subtraction of (160550 - 160180) = 370. It would be impossible to say how many hours are missing without a calculation evaluating the amount spent per km/h driven.

  • Forget this post that Oce commented here, see the other one that I sent you the link and see if you can understand it

  • I added the comment on the post there, see if it solves?

Show 4 more comments

Browser other questions tagged

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