SQL查询以获取排除已获取项目的项目

时间:2018-04-12 05:21:08

标签: sql-server linq

我需要sql查询从表中获取随机项目,不包括先前以相同方式提取的项目。

1 个答案:

答案 0 :(得分:0)

我试图以我能想到的最佳方式回答您的解决方案。然而,仍有一些不明确的问题,我没有考虑到这些问题;

  
      
  1. 当您用完之前选择的值时,您会怎么做?然后你什么都不回来?在某些时候,您已经随机选择了所有值。

  2.   
  3. 应选择多少项目以及“之前”的具体用语

  4.   

测试数据

declare @randomitem nvarchar(50)
declare @firstrun int = (select count(*) from dbo.randomlist)



if(@firstrun = 0)
BEGIN
SELECT top 1 @randomitem=[Names]
  FROM   [LegOgSpass].[dbo].[Randoms]

  order by newid();

truncate table dbo.randomitems;
insert into dbo.randomitems
select @randomitem
;

select * from dbo.randomitems a
where not exists (select * from dbo.randomlist b where a.namesused = b.namesused);

insert into dbo.randomlist
select @randomitem;
END

if(@firstrun >= 1)
BEGIN
SELECT top 1 @randomitem=[Names]
  FROM   [LegOgSpass].[dbo].[Randoms]  a
  where not exists (select * from dbo.randomlist b where a.Names = b.namesused)

  order by newid();


truncate table dbo.randomitems;
insert into dbo.randomitems
select @randomitem
;

select * from dbo.randomitems a
where not exists (select * from dbo.randomlist b where a.namesused = b.namesused);

insert into dbo.randomlist
select @randomitem;
END

SQL脚本

select * from  dbo.Randoms
select * from  dbo.randomitems
select * from  dbo.randomlist

<强>选择

interface HttpProgressEvent { 
  type: HttpEventType.DownloadProgress | HttpEventType.UploadProgress
  loaded: number
  total?: number
}