我试图解决的问题加入行的更好的方法是知道密码怎么会经常occure相同registrationid和33以及limitid值5基于相同的ID
这是一对夫妇我在SQL数据库表中有以下行:
id limitid registrationid
---------------------------------------
113 33 3712
114 5 3712
116 33 3713
117 5 3713
1428 33 3714
1434 5 3720
RegistrationId在此表中可以有0,1或2个值。 我要寻找其中regsitrationId有两个值的情况下,并(与registrationid一起)把相应limitid的结果集中,如:
limitid1 limitid2 registrationid
------------------------------------------
33 5 3712
为了实现这个目标我使用这个SQL代码:
DECLARE @t TABLE(
id int NOT NULL,
regid int NOT NULL,
limitid int not null
);
insert into @t
SELECT Id, RegistrationId, limitid
FROM my-table
WHERE (LimitId = 33)
ORDER BY RegistrationId
DECLARE @t2 TABLE(
id int NOT NULL,
regid int NOT NULL,
limitid int not null
);
insert into @t2
SELECT Id, RegistrationId, limitid
FROM my-table
WHERE (LimitId = 5)
ORDER BY RegistrationId
declare @t3 Table(
regid int NOT NULL,
limitid1 int not null,
limitid2 int not null
);
insert into @t3
SELECT t1.RegId, t1.limitid, t2.limitid
FROM @t as t1
join @t2 as t2
on t1.regid = t2.regid
select * from @t3
我的问题:是否有不同的/更好的方法来达到相同的结果?
谢谢!
你应该更好地解释你正在尝试做什么。我不认为这很清楚。 –
@FilipeSilva我编辑了我的问题。 – Roger