Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is actually a fairly sane setup to query. Personally, I'd recommend following @inhan's advice, especially if it turns out cars can be booked for smaller slices of time (hours?). However, the queries get a little bit more complicated to write, because of edge-cases (if you look around here, you should find relevant examples).</p> <p>Unfortunately, I can't speak to how this fits into your framework, but the SQL can look like this: </p> <pre><code>SELECT Cars.car_id, Cars.name, Cars.description FROM Cars JOIN (SELECT car_id FROM Cars_Availability WHERE `date` &gt;= '2013-02-05' AND `date` &lt; '2013-02-09' AND status = 1 GROUP BY car_id HAVING COUNT(*) = DATEDIFF('2013-02-09', '2013-02-05')) Available ON Available.car_id = Cars.car_id </code></pre> <p>(Have an <a href="http://sqlfiddle.com/#!2/5f970/2" rel="nofollow">SQL Fiddle Example</a>)<br> This query assumes that there's only one entry per day, or this breaks. Basically, it grabs every 'available' row greater-than/equal the beginning date, and less than the 'end' date (date/time/timestamps are... complicated; <a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/what-do-between-and-the-devil-have-in-common.aspx" rel="nofollow">this article</a> deals with SQL Server-specific problems, but the basic concepts apply), and makes sure there's one for the count of days between them. Mostly, you were looking for the <code>HAVING</code> clause.<br> Given the proper index on <code>Cars_Availability</code>, it may not even hit the table, making this a good performer, too.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload