I have a database with two tables. One is the coche
table and the ruta
table:
Table coche
:
pk_coche modelo
-------------------
1 Modelo 1
2 Modelo 2
3 Modelo 3
Table ruta
:
pk_ruta fk_coche kms_inicio kms_final
-------------------------------------------------
1 1 200.000 210.000
2 3 300 0
The column of kms_inicio
refers to the kilometers with which a car starts the route, for example with 200.000 kms
. As long as the column of kms_final
is in 0
, it means that that car is en route.
Well, my problem comes when I create a sql statement so that I can return the cars that are not en route, so that there are not two cars in the same route. As you can see the car Modelo 2
, it is not en route so with my sentence if it appears, the problem is that Modelo 1
should appear since that car has finished its route. But with my sentence I only see Modelo 2
, which has not started any route. My sentence is this:
SELECT pk_coche, modelo FROM coche v
left JOIN ruta r ON v.pk_coche=r.fk_coche
WHERE r.pk_ruta IS NULL
Any help?