dimanche 2 juin 2019

how to find the available cars with query sql

i want to select all the cars that are available between two date , my query doesn't work for all possibility. this is the table of cars

(`agence_ID`, `MATRICULE`, `MARQUE_ID`, `CATEGORIE_ID`, `TYPE_ID`, `KM_COMPTEUR`, `COULEUR`, `MOTEUR`, `PRIX_SANS_CHAUFFEUR`, `PRIX_AVEC_CHAUFFEUR`, `nom_voiture`) VALUES
(1, '5cc7865d214d91.78324437', 4, 5, 0, 2352200, 'blanche', '1.6   Diesel', 4500, NULL, 'yaris'),
(1, '5cc76c0fd05a79.56873077', 5, 4, 0, 2425, 'lk', 'Essence 12', 14, NULL, '214'),
(1, '5cc76f609c6d21.16821514', 3, 1, 0, 554, 'ghk', 'Essence 1.635', 2352, NULL, '2545'),
(1, '5cc770e69054d8.95558934', 5, 1, 0, 2366585, 'gold', 'Essence 1.6 dci ', 255556000, NULL, '528');

four cars and this is the reservations table (3 reservations , 2 for the same cars/vehicule/; which cause the problem)

''
`AGENCE_ID`, `CLIENT_ID`, `MATRICULE`, `DATE_DEBUT`, `DATE_FIN_PREVUE`, `DATE_FIN_REELLE`, `KM_DEBUT`, `KM_FIN`, `DATE_RESERVATION`, `PRIX_JOURNALIER`, `OBSERVATION`) VALUES
(1, 12, '5cc7865d214d91.78324437', '2019-06-02', '2019-06-02', NULL, 444444, NULL, '2019-06-02', 300, NULL),
(1, 13, ' 5cc7865d214d91.78324437', '2019-06-03', '2019-06-03', NULL, 455555, NULL, '2019-06-02', 3000, NULL),
(1, 13, '5cc76c0fd05a79.56873077', '2019-06-13', '2019-06-27', NULL, 455555, NULL, '2019-06-02', 44444444, NULL);'' 

my query doesnt work for this example and return the 4 cars

SELECT *FROM vehicule C WHERE c.agence_ID= '1' AND NOT EXISTS (SELECT DISTINCT * FROM reservation r WHERE r.MATRICULE=C.MATRICULE AND (('2019-06-03' BETWEEN r.DATE_DEBUT  AND r.DATE_FIN_PREVUE)or ('2019-06-03'BETWEEN r.DATE_DEBUT AND r.DATE_FIN_PREVUE)))   




Aucun commentaire:

Enregistrer un commentaire