How to Subtract MYSQL

Asked

Viewed 221 times

0

How to do the correct subtraction of these two columns in the SQL script?

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

inserir a descrição da imagem aqui

  • 2
  • follow the consultation

  • SELECT DISTINCT b.prefixo as idVeiculo, max(datatroca) as datatroca, max(kmtroca) as kmtroca, max(horimetroca) as horimetroca, max(proximatroca) as proximatroca, max(c.horimetro) as horimetroatual, (a.proximatroca - c.horimetro) as horarestantes, idMecanico FROM oleomotor a
JOIN vehicles b on (a.idVeiculo=b.idVeiculo) JOIN hodometro c on (a.idVeiculo=c.idVeiculo) GROUP BY a.idVeiculo order by datatroca DESC;

  • That solves? --> (max(a.proximatroca) - max(c.horimentro) as Falta)

  • did not resolve...

1 answer

0

In the mysql you must use the timediff for hours comparison:

SELECT DISTINCT b.prefixo as idVeiculo,
                max(datatroca) as datatroca, 
                max(kmtroca) as kmtroca,
                max(horimetroca) as horimetroca,
                max(proximatroca) as proximatroca,
                max(c.horimetro) as horimetroatual,
                timediff(a.proximatroca, c.horimetro) as Falta,
                idMecanico FROM oleomotor a
JOIN veiculos b on (a.idVeiculo=b.idVeiculo)
JOIN hodometro c on (a.idVeiculo=c.idVeiculo)
GROUP BY a.idVeiculo order by datatroca DESC;
  • was solved thus (a.proximatroca - max( c.horimetro)) as Missing

Browser other questions tagged

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