Solved

MYSQL QUERY - if another column is true in the query

Posted on 2016-09-05
9
46 Views
Last Modified: 2016-09-06
I have a query which pulls out data from a booking database.
It lists all the possible booking options as well and if the user has booked it.

The booking options have a topicID column, some options have the same topicID and cannot be booked twice.

I want to be able to add a column to my query which shows if the user has booked each topic... Perhaps by adding a topicBooked True/False column.

Current query example:
SELECT gb.* , bo.*, gb.bookingGroupId,
EXISTS ( SELECT * FROM user_bookingOption ubo WHERE bo.bookingOptionID =  ubo.bookingOptionID AND ubo.userId = 2112) as userHasBooked
FROM bookingGroup gb
LEFT JOIN bookingOption bo ON (gb.bookingGroupId = bo.bookingGroupId)

For each row I need to ask has there been an option booked with the same topic number in this query.

Any help would be much appreciated.
0
Comment
Question by:Steve Tinsley
  • 4
  • 3
  • 2
9 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785440
if a user has booked a topic (topicBooked), isn't it that mean a topic is booked (topicBooked) too?

is the table: bookingGroup stores the topic info?
0
 
LVL 24

Expert Comment

by:chaau
ID: 41785534
I think you can add this column:

, EXISTS ( SELECT * FROM user_bookingOption ubo 
           WHERE bo.bookingOptionID <>  ubo.bookingOptionID AND 
           bo.topicID =  ubo.topicID AND ubo.userId = 2112) as topicBooked 

Open in new window


This will have a true value when there is the same TopicID exists for a different booking
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41785575
just realized I got a typo, what i mean is:

if a user has booked a topic (userHasBooked), isn't it that mean a topic is booked (topicBooked) too?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Steve Tinsley
ID: 41785639
Hi Guys,
Thanks for trying to help... I was worried i wasn't explaining the issue.

RYAN - Yes if a user has booked a topic (userHasBooked), then they HAVE booked (topicBooked) too.
- There can be multiple options of the same topic, so I want a flag next to all the topics when 1 of them has been booked. eg:

option | topicId | userHasBooked | topicBooked
option1      1                        0                           1
option2      2                        1                           1
option3      3                        0                           0
option4      1                        1                           1
option5      1                        0                           1
option6      3                        0                           0
option7      2                        0                           1

In this mock up you can see they have booked option2 and option4 which are in toipcId 2&1. Means I want topicBooked to be true for all the topics 2 and 1... but topicId 3 hasnt been booked yet so topicBooked is false for that..
0
 

Author Comment

by:Steve Tinsley
ID: 41785644
Chaau,
Thanks for your suggestion.... I think you understand what I want.... BUT:
The confusing thing is that the topicID is only stored in bookingOption... so I somehow need to cross reference if a booking has been made with bookingOption.topicID??????

Any other thoughts
0
 

Author Comment

by:Steve Tinsley
ID: 41785647
Heres a screenshot of the db:database
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 41787051
Thanks for the table design screenshot. I think this will work:
, EXISTS ( SELECT 1 FROM user_bookingOption ubo INNER JOIN bookingOption bo1
           ON ubo.bookingGroupId = bo1.bookingGroupId
           WHERE bo.bookingOptionID <>  ubo.bookingOptionID AND 
           bo.topicID =  bo1.topicID AND ubo.userId = 2112) as topicBooked 

Open in new window

0
 

Author Comment

by:Steve Tinsley
ID: 41787293
Hi Chaau,

YES! This is 99% there for what I want.

I changed ON ubo.bookingGroupId = bo1.bookingGroupId to ON ubo.bookingOptionId = bo1.bookingOptionId

And then I decided to remove: WHERE bo.bookingOptionID <>  ubo.bookingOptionID
as I wanted booked items to also show as topicBooked=1 as well.

Would you say that is OK??

Heres the altered query:
, EXISTS ( SELECT 1 FROM user_bookingOption ubo INNER JOIN bookingOption bo1
           ON ubo.bookingOptionId = bo1.bookingOptionId
           WHERE bo.bookingOptionId <>  ubo.bookingOptionId AND 
           bo.bookingOptionTopic =  bo1.bookingOptionTopic AND ubo.userId = 2112) as topicBooked 

Open in new window

0
 
LVL 24

Expert Comment

by:chaau
ID: 41787294
Yes, should be fine, if it does the job.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question