Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn rows which don't have any matches in multiple-join situation
    primarykey
    data
    text
    <p>I must admit I'm pretty bad when it comes to SQL (MySQL in this case) so I apologise if this is simple!</p> <p>I have the following query:</p> <pre><code>SELECT b.tourrefcode, t.startdate, SUM(a.adults + a.children + a.infants) as Pax FROM explorer.booking_record b INNER JOIN explorer.bookingroom a ON a.bookingref = b.bookingref LEFT JOIN explorer.tour t ON b.tourrefcode = t.tourcode AND (t.startdate &gt; '2012-02-01' AND t.startdate &lt; '2012-03-01') AND (b.bookingstatus = "P" OR b.bookingstatus = "F") AND (b.tourdeparturedate &gt; '2012-02-01' AND b.tourdeparturedate &lt; '2012-03-01') GROUP BY t.tourcode HAVING SUM(a.adults + a.children + a.infants) &lt; 120 </code></pre> <p>The idea is that the query returns every tour from explorer.tour along with a count of the passengers (Pax in this case) for every booking found in booking_record. Booking_record is linked to bookingroom which contains the adults/children/infant figures.</p> <p>As such, I'm having to join booking_reference with bookingroom to get the figures I'm looking for. </p> <p>The query itself works perfectly and gives me the correct passengers for the correct tour on the correct date. However, it will not show me those tours in which there have been no bookings.</p> <p>I've tried a number of different constructions of the same query including every type of join and moving the clauses from WHERE into the ON (as shown in the current implementation).</p> <p>Any help would be massively appreciated!</p> <p>EXPLAIN as requested:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t index PRIMARY 11 58415 Using index; Using temporary; Using filesort 1 SIMPLE a ALL PRIMARY 718736 1 SIMPLE b eq_ref PRIMARY PRIMARY 5 explorer.a.bookingref 1 </code></pre> <p>Regards, Daniel.</p>
    singulars
    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. This table or related slice is empty.
    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