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

  • Home
  • SEARCH
  • 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 7657525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:03:24+00:00 2026-05-31T13:03:24+00:00

Tables: bookings : id, user_id, object_id, date1, … booking_status : id, book_id, status status

  • 0

Tables:

bookings: id, user_id, object_id, date1, …

booking_status: id, book_id, status

status is int, range from 1 to 9 (request, confirmed, paid, cancelled by user and that sort of stuff), so I need all bookings, where status is at least 4 (which means paid) but no value bigger than 4 (which would mean cancelled etc).

Till now the SELECT looks about like this (I left out some fields (…) to shorten it):

SELECT b.date1, ..., u.name FROM bookings b
    LEFT JOIN user u ON (b.user_id = u.id)
    LEFT JOIN booking_status bs ON (b.id=bs.book_id)
  WHERE ((b.object_id=$object_id) AND (bs.status NOT IN (5,6,7,8,9)));"

…but it still selects those bookings that have booking status bigger than 4 as well. Any ideas how I need to change the query??

Thank you very much in advance!


UPDATE: thank you all again, I am amazed with how many great ideas you have come up with! There is really many ways to do it and I learned a lot from you, so thank you again! I will try all your suggestions and see for the performance, for now I mixed your solutions to this query, which works for now but I need to test it further:

SELECT b.date, ..., u.name FROM bookings b
LEFT JOIN user u ON (b.user_id = u.id)
LEFT JOIN booking_status bs ON (b.id=bs.book_id AND bs.status<=4)
WHERE (b.object_id=$object_id) HAVING MAX(bs.status)=4

it does not return multiple rows, but returns the rows with 4, excludes the rows with more than 4 and has no subqueries…

EDIT 2: I edited the query again… with HAVING MAX(bs.status)=4 it then works…

EDIT 3: sorry, after testing different cases I have to admit I was much too fast by saying it works…

  • 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-31T13:03:26+00:00Added an answer on May 31, 2026 at 1:03 pm

    inspired by ruakh’s solution without the correlated subqueries:

    select ...
    from bookings b
    join (select book_id from booking_status group by book_id having max(status) = 4
         ) bs on b.id = bs.book_id
    left join user u on b.user_id = u.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables, with these keys in my database: bookings session_id sessions
With tables basically like this: Elements id INT PRIMARY KEY ... Observations id INT
The Tables Currencies ---------- CurrencyId INT IDENTITY PK IsoCode4217 CHAR(3) -- USD, GBP, EUR,
var grid = new System.Web.UI.WebControls.GridView(); grid.DataSource = from booking in bookings join f in
From reading the title it might seem like an odd request, so let me
I'm developing an ASP.Net MVC site and on it I list some bookings from
I have a two tables events and bookings, which are linked by event_id. Example:
I've got two tables, one for openings and the other for bookings. An entry
I have a mysql database with two tables, bookings and users. They are linked
I have Memberships and Bookings tables in a database containing an attribute cust_id, which

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.