I’m developing an odometer application to keep track how much kilometers I drive everyday, this results are stored in a database. Since my car doesn’t give how many kilometeres where driven by day, I subtract total kilometers from today kilometers.
Let’s say that total kilometers are 42,904. I get that value with a query something like this:
SELECT SUM(kilometros) FROM `kilometraje` WHERE id_vehiculo =2
But today my odometer shows 42,967 km, then I manually do the subtraction and insert the difference in the database .
INSERT INTO kilometraje (id_vehiculo, kilometros, fecha) VALUES (2, 63, '2013-01-14')
How do I achieve this process in a single query?, I’ve tried the following without success:
INSERT INTO kilometraje (id_vehiculo, kilometros, fecha) VALUES (2, 42967-(SELECT SUM(kilometros) FROM `kilometraje` WHERE id_vehiculo =2) , '2013-01-14')
Because I get this error:
– You can’t specify target table ‘kilometraje’ for update in FROM clause
use
INSERT..INTO SELECT