sql >> Database teknologi >  >> RDS >> Mysql

HQL / JPA finde tilgængelige varer mellem datointerval

Jeg kan se, at din forespørgsel er korrekt.

Men hvis du vil vælge baseret på bil-id, bør du også tilføje bil-id som en betingelse i din forespørgsel

For eksempel

SELECT b.id, b.startDate, b.endDate, b.car.id
FROM BorrowedDate b
WHERE :userDateStart NOT BETWEEN b.startDate AND b.endDate
AND :userDateEnd NOT BETWEEN b.startDate AND b.endDate
AND b.car.id = :carId

ELLER du kan også gøre det sådan

SELECT b.id, b.startDate, b.endDate, c.id
FROM BorrowedDate b INNER JOIN b.car c
WHERE :userDateStart NOT BETWEEN b.startDate AND b.endDate
AND :userDateEnd NOT BETWEEN b.startDate AND b.endDate
AND c.id = :carId

så skal du bestå :cardId værdi er lig med 2

OPDATERING

du kan prøve denne SQL-forespørgsel, hvis du har brug for at få resultatet dynamisk, jeg opdaterer det snart med JPQL

SELECT b.id, b.startDate, b.endDate, b.car.id
FROM BorrowedDate b
WHERE '2017-04-02 00:00:00' NOT BETWEEN b.startDate AND b.endDate
AND '2017-04-10 00:00:00' NOT BETWEEN b.startDate AND b.endDate
AND b.car.id NOT IN (SELECT DISTINCT bd.car.id FROM BorrowedDate bd WHERE '2017-04-02 00:00:00' BETWEEN bd.startDate AND bd.endDate OR '2017-04-10 00:00:00' BETWEEN bd.startDate AND bd.endDate) 

HER er JPQL også

SELECT model.id, model.startDate, model.endDate, model.car.id
FROM BorrowedDate model
WHERE :userDateStart NOT BETWEEN model.startDate AND model.endDate
AND :userDateEnd NOT BETWEEN model.startDate AND model.endDate
AND model.car.id NOT IN (SELECT DISTINCT b.car.id FROM BorrowedDate b WHERE :userDateStart BETWEEN b.startDate AND b.endDate OR :userDateEnd BETWEEN b.startDate AND b.endDate)


  1. PostgreSQL Opret indeks

  2. Rails schema.rb inkluderer ikke ny tilpasset Postgres-funktion

  3. Tilsluttet forespørgsel mangler poster

  4. Sådan fungerer JulianDay()-funktionen i SQLite