How to do MYSQL subtraction

0

How to correctly subtract 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;

    
asked by anonymous 01.05.2018 / 23:45

1 answer

0

In mysql you should use timediff for comparison of hours:

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;
    
02.05.2018 / 13:21