简体   繁体   English

如何联接两个表并获得正确的记录?

[英]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: 这是我的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: 这是我的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. 如您所见,我只想为ID为162的用户显示时隙并显示记录,而不同时为ID为15的用户显示记录。 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: 因此,在那之后,我尝试将查询限制在User_ID上,我得到了:

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? 所以我的问题是我如何才能获得所有时隙,但同时要限制对我想要的User_ID的查询? Is something like that even possible in sql? 有没有可能在sql中实现类似的功能? If anyone can help with this problem please let me know. 如果有人可以解决这个问题,请告诉我。 Thanks in advance. 提前致谢。

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. 在使用WHERE时,将限制最终结果,在这种情况下,您将退出外部联接,然后仅选择带用户的项目。 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. 您需要使用LEFT JOIN的ON子句来有选择地进行连接,同时保留来自第一个表的原始记录。

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'

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. 您需要使用您要使用的用户ID编辑第8行,并使用您要使用的事件ID编辑第9行。

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

So basically you're joining the tables on two pieces of information: 因此,基本上,您要在两个信息上连接表格:
1) The Slot_ID 1)Slot_ID
2) The Event_ID 2)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. 例如:您想获得有关事件= 150和槽位时间= 6的信息的行。以这种方式获得表之间的链接后,您将希望限制通过特定事件获得的值(150)和一个特定的用户。 As cantSleepNow has said, it's impossible for you to get that user value in a column where you have time values. 正如cantSleepNow所说,您不可能在具有时间值的列中获得该用户值。 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. 这样您就可以看到时间段ID,事件ID,实际时间段以及前往该事件的用户。

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM