espresso_coffee
espresso_coffee

Reputation: 6110

How to join two tables and get correct records?

I'm working on project where I have to combine records from two different tables and display them on the screen based on the parameters. My first table contain time slot records. Here is example of my SLOT_TABLE:

ID  Event_ID       Time_Slots
 1    150       7:00 AM - 7:15 AM
 2    150       7:15 AM - 7:30 AM
 3    150       7:30 AM - 7:45 AM
 4    150       7:45 AM - 8:00 AM
 5    150       8:00 AM - 8:15 AM
 6    150       8:15 AM - 8:30 AM

My second table contain records for each user. Here is example of my REGISTRATION_TABLE:

ID   Event_ID   Supervisor_ID  Slot_ID  User_ID  Staff_ID
61     150           200         6        15       133
78     150           200         6        162      79

I have a problem to display all my time slots but with the records from the second table just for specific User. Here is example how I would like my records to be displayed:

ID  Event_ID       Time_Slots         User_ID
 1    150       7:00 AM - 7:15 AM
 2    150       7:15 AM - 7:30 AM
 3    150       7:30 AM - 7:45 AM
 4    150       7:45 AM - 8:00 AM
 5    150       8:00 AM - 8:15 AM
 6    150       8:15 AM - 8:30 AM      162

As you can see I would like to display my time slots and display record just for my user with an id of 162, but not user of id 15 at the same time. I try to use this query to get that:

Select s.Time_Slots, r.User_ID  
From SLOT_TABLE s
Left Outer Join REGISTRATION_TABLE r
On s.ID = r.SLOT_ID
Where s.EVENT_ID = '150'

But query above gave me this:

ID  Event_ID       Time_Slots          User_ID
 1    150       7:00 AM - 7:15 AM
 2    150       7:15 AM - 7:30 AM
 3    150       7:30 AM - 7:45 AM
 4    150       7:45 AM - 8:00 AM
 5    150       8:00 AM - 8:15 AM
 6    150       8:00 AM - 8:15 AM        162
 6    150       8:00 AM - 8:15 AM        15

So after that I tried to limit my query on User_ID and I got this:

Select s.Time_Slots, r.User_ID  
From SLOT_TABLE s
Left Outer Join REGISTRATION_TABLE r
On s.ID = r.SLOT_ID
Where s.EVENT_ID = '150'
And User_ID = '162'

 ID  Event_ID       Time_Slots                 User_ID
 6    150        8:00 AM - 8:15 AM               162

So my question is how I can get all time slots but at the same time to limit my query on User_ID that I want? Is something like that even possible in sql? If anyone can help with this problem please let me know. Thanks in advance.

Upvotes: 2

Views: 77

Answers (3)

Josh Gilfillan
Josh Gilfillan

Reputation: 5146

Try this. You need to edit line 8 with the user ID you are after and line 9 with the event ID you are after.

select a.id
  ,a.event_id
  ,case when b.user_id is not null then 'User_ID(' + b.user_id + ')' else a.time_slots end as time_slots
from slot_table a
left join registration_table b
on a.event_id = b.event_id
and b.slot_id = a.id
and b.user_id = '162'     -- parameter 1
where a.event_id = '150'  -- parameter 2

Edit: the above code will work for the original requirement, the below code for the new requirement:

select a.id
  ,a.event_id
  ,a.time_slots
  ,b.user_id
from slot_table a
left join registration_table b
on a.event_id = b.event_id
and b.slot_id = a.id
and b.user_id = '162'     -- parameter 1
where a.event_id = '150'  -- parameter 2

Upvotes: 2

Dresden
Dresden

Reputation: 559

So basically you're joining the tables on two pieces of information:
1) The Slot_ID
2) The Event_ID

For example: You want to get rows that have information regarding event = 150 and slot time = 6. After you get the link between the tables that way you're going to want to limit your values you get by the specific event (150) and a specific user. As cantSleepNow has said, it's impossible for you to get that user value in a column where you have time values. So what you're most likely looking for for formatting is this:

ID | EVENT_ID | TIME_SLOTS | USER_ID

So you can see the time slot ID, the event ID, the actual time slot and the user going to that event.

So most likely this:

SELECT ST.ID, ST.EVENT_ID, ST.Time_Slots, RT.User_ID
FROM SLOT_TABLE AS ST
LEFT OUTER JOIN REGISTRATION_TABLE AS RT 
ON ST.ID = RT.SLOT_ID AND ST.EVENT_ID = RT.EVENT_ID
WHERE ST.EVENT_ID = '150' AND RT.User_ID = 'XXX'

Upvotes: 1

Siderite Zackwehdex
Siderite Zackwehdex

Reputation: 6580

When you use WHERE you limit the final result, in your case you left outer join and then you select only the items with user. You need to use the ON clause of the LEFT JOIN in order to selectively join, while keeping the original records from the first table.

Maybe like this:

Select s.Time_Slots, r.User_ID  
From SLOT_TABLE s
Left Outer Join REGISTRATION_TABLE r
On s.ID = r.SLOT_ID
-- here, in the ON clause, not in WHERE
And User_ID = '162'
--probably you also want to check the registration table EVENT_ID?
AND s.EVENT_ID=r.EVENT_ID
Where s.EVENT_ID = '150'

Upvotes: 3

Related Questions