日期:2014-05-17 浏览次数:20395 次
(select top 3 Voter from tblVOTEVoteRecord a left join tblVOTEActivityInfo b on a.ActivityID=b.ActivityID where a.ActivityID='43bf7430dbae435ca0c1f4856eb8cefa' order by newid()) from tblVOTEDrawingInfo where DrawingID=‘222’)
------解决方案--------------------
INSERT INTO tblVOTEWinnerList ([ID] ,[DrawingID] ,[ActivityID] ,[RulesID] ,[PhoneNumber] ) ( select top 3 '111',222,'333','444', PhoneNumber =(select Voter from tblVOTEVoteRecord a left join tblVOTEActivityInfo b on a.ActivityID=b.ActivityID where a.ActivityID='43bf7430dbae435ca0c1f4856eb8cefa' order by newid()) from tblVOTEDrawingInfo where DrawingID=‘222’)
------解决方案--------------------
取前一名,子查询读取一条记录,然后插入;
取多名,就循环下,分别插入。
给个大概意思,自己改改吧。
比如取 3条,插入
declare @i int---计数器 declare @n int declare @str nvarchar(50) set @i=1 select top 3 Voter from tblVOTEVoteRecord a left join ......--你那串子查询 select @n=@@rowcount while @i<=@n begin set @str=(select Voter from #t where id=@i) insert into tblVOTEWinnerList(.......) values(,,,@str) set @i=@i+1 end
------解决方案--------------------
忘了临时表了,,加上
declare @i int---计数器 declare @n int declare @str nvarchar(50) set @i=1 select top 3 Voter into #t from tblVOTEVoteRecord a left join ......--你那串子查询 select @n=@@rowcount while @i<=@n begin set @str=(select Voter from #t where id=@i) insert into tblVOTEWinnerList(.......) values(,,,@str) set @i=@i+1 end
------解决方案--------------------
INSERT INTO tblVOTEWinnerList ([ID] ,[DrawingID] ,[ActivityID] ,[RulesID] ,[PhoneNumber] ) SELECT '111' , 222 , '333' , '444' , voter AS PhoneNumber FROM ( SELECT TOP 3 Voter FROM tblVOTEVoteRecord a LEFT JOIN tblVOTEActivityInfo b ON a.ActivityID = b.ActivityID WHERE a.ActivityID = '43bf7430dbae435ca0c1f4856eb8cefa' AND DrawingID = '222' ORDER BY NEWID() ) a
------解决方案--------------------
INSERT INTO tblVOTEWinnerList ([ID] ,[DrawingID] ,[ActivityID] ,[RulesID] ,[PhoneNumber] ) ( select top 3 '111',222,'333','444',PhoneNumber = Voter from tblVOTEVoteRecord a left join tblVOTEActivityInfo b on a.ActivityID=b.ActivityID where a.ActivityID='43bf7430dbae435ca0c1f4856eb8cefa' order by newid()) from tblVOTEDrawingInfo where DrawingID=‘222’