Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:27:42+00:00 2026-05-24T18:27:42+00:00

I must admit I’m pretty bad when it comes to SQL (MySQL in this

  • 0

I must admit I’m pretty bad when it comes to SQL (MySQL in this case) so I apologise if this is simple!

I have the following query:

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 > '2012-02-01' AND t.startdate < '2012-03-01') AND (b.bookingstatus = "P" OR b.bookingstatus = "F") AND (b.tourdeparturedate > '2012-02-01' AND b.tourdeparturedate < '2012-03-01')
GROUP BY t.tourcode
HAVING SUM(a.adults + a.children + a.infants) < 120

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.

As such, I’m having to join booking_reference with bookingroom to get the figures I’m looking for.

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.

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).

Any help would be massively appreciated!

EXPLAIN as requested:

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   

Regards, Daniel.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:27:43+00:00Added an answer on May 24, 2026 at 6:27 pm

    You are mixing up your left/right joins:

    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
    RIGHT JOIN explorer.tour t ON b.tourrefcode = t.tourcode AND (t.startdate > '2012-02-01' AND t.startdate < '2012-03-01') AND (b.bookingstatus = "P" OR b.bookingstatus = "F") AND (b.tourdeparturedate > '2012-02-01' AND b.tourdeparturedate < '2012-03-01')
    GROUP BY t.tourcode
    HAVING SUM(a.adults + a.children + a.infants) < 120
    

    I changed one join to a RIGHT JOIN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a bit of an odd case I must admit, but I'm trying
I must admit, this is my first post on this site, so I apologise
I have just installed Microsoft Robotics Studio 2008 R2, and I must admit that
I must admit I have researched a bit but keep coming into a block
first i must admit that i'm not very familiar with sql server's recursive CTE's
I must admit that I am incredibly jealous of those developers who happen to
Accepting the possibility of extreme ridicule, I must admit that I really miss sun
I must admin this is kind of funny even though I think I understand
Must Support IE6 and must Validate vs XHTML Strict 1.0! This is tricky to
This must be a classic .NET question for anyone migrating from Java. .NET does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.