MySQL Select Query

0

First of all good afternoon everyone. To the point !!

I have a table called "control" in which I have registered the kilometers of several vehicles, all well at the time of inserting delete etc.

But I am trying to bring the data of a vehicle determined by its registration; This data is named "KmRest" , that is (Remaining Mileage) . For example, I explain how the question works: I have the following fields: KmInicial, KmFinal, KmReco, ProxMaintenance and KmRest the process is the following: KmInicial = 100 KmFinal = 200 KmReco = 100 (The difference between KmInicial and KmFinal) ProxMaintenance = 5000 KmRest = 5000 - 100 KmRest = 4900

So far everything is fine but by continuing to enter data the KmRest will reach "0" when this happens my programming will indicate that it is time for a maintenance BUT! I have an SQL query that throws at me < strong> "KmRest" of the last insert in this way: "SELECT min(kmrest) AS Restantes from control WHERE matricula = ? order by max(fecha)"

This works to a certain point but when I subtract again to show the KmRest this does not bring me the new and last value but the previous one here an example:

Here is my table in my programming showing the data that should not be:

If someone can help me, I appreciate it for a University project.

    
asked by Riddick 28.09.2017 в 21:20
source

1 answer

0

Where you get Matricula I do not know, as far as I see the field is plate. and this query should work for you

SELECT kmrest FROM control WHERE placa = '?' ORDER BY id DESC LIMIT 1;

I really do not remember if LIMIT goes after ORDER BY or before in MYSQL, tests and comments profa.

Greetings

    
answered by 28.09.2017 / 22:17
source