我有两张桌子:
request
:请求数据的内容participant
:邀请的员工的内容表Request
:
ID_Request emp_no invited_by
-------------------------------
BTR1370 101240 101240
BTR1371 100259 101240
BTR1372 100308 101240
BTR1373 100626 101240
表Participant
:
ID_Participant ID_Request participant_no invited_by
------------------------------------------------------
1 BTR1370 100259 101240
2 BTR1370 100308 101240
3 BTR1370 100626 101240
我想加入这些表格,并期望在此处获得此结果:
ID_Request participant_no invited_by
---------------------------------------
BTR1370 100259 101240
BTR1370 100308 101240
BTR1370 100626 101240
BTR1371 100259 101240
BTR1371 100308 101240
BTR1371 100626 101240
BTR1372 100259 101240
BTR1372 100308 101240
BTR1372 100626 101240
BTR1373 100259 101240
BTR1373 100308 101240
BTR1373 100626 101240
我如何加入这两张桌子?我正在使用SQL Server 2012。
我非常感谢你的帮助
答案 0 :(得分:0)
使用交叉连接
select TR.ID_Request,TR.participant_no,TR.invited_by from Table_Request TR cross join Table_Participant TP where TR.invited_by = TP.invited_by
答案 1 :(得分:0)
使用应该使用CROSS JOIN就像Gordon linoff所说: 你可以包含一个where子句,使其行为像内连接 - 即。其中a.invited_By = b.invited_by
declare @myt1 table (id_request nvarchar(50),emp_no int, invited_by int)
insert into @myt1
values
('BTR1370', 101240 ,101240),
('BTR1371', 100259 ,101240),
('BTR1372', 100308 ,101240),
('BTR1373', 100626 ,101240)
declare @myt2 table ( id_participant int,id_request nvarchar(50),participant_no int, invited_by int)
insert into @myt2
values
(1 ,'BTR1370', 100259 , 101240 ),
(2 ,'BTR1370', 100308 , 101240 ),
(3 ,'BTR1370', 100626 , 101240 )
select a.id_request,b.participant_no,a.invited_by from @myt1 a
cross join @myt2 b
order by a.id_request
<强>结果强>
答案 2 :(得分:0)
如果CROSS JOIN不是您所追求的,并且无法看到更多样本数据,我只能假设您想要内部联接
SELECT r.ID_Request, p.participant_no, r.invited_by
FROM Table_Request r
INNER JOIN Table_Participant p
ON r.ID_Request = p.ID_Request