当前位置: 代码迷 >> Sql Server >> 求不存在记录,该怎么处理
  详细解决方案

求不存在记录,该怎么处理

热度:66   发布时间:2016-04-25 00:22:04.0
求不存在记录
A表
E_Word nvarchar(400)
E_HY nvarchar(20)
E_LAN nvarchar(8)
三个字段

B表也是这个三个字段
E_Word nvarchar(400)
E_HY nvarchar(20)
E_LAN nvarchar(8)

我用
select top 20 *  from A表  order by newid(); 
//从A表随即取出20条 放到B表里去
//下次再取的时候 如果B表里存在 就不取了
//这个SQL语句怎么写?
//因为这是随即取的 我不知道怎么写才效率高

------解决方案--------------------
select top 20 *  from A表  
except
select * from B表
order by newid(); 
------解决方案--------------------
  select top 20 * from 
  (
  select E_Word,E_HY,E_LAN from A
  except
  select E_Word,E_HY,E_LAN from B) as TB
  order by NEWID()

------解决方案--------------------
  select top 20 * from 
  (
  select E_Word,E_HY,E_LAN from A
  except
  select E_Word,E_HY,E_LAN from B) as TB
  where xxx='xxx'
  order by NEWID()

------解决方案--------------------
给抢了...
select top 20 * from a 
where not exists (select 1 from b where a.E_Word=b.E_Word and a.E_HY =b.E_HY  and  a.E_LAN=b.E_LAN)
order by newid()
1楼的要2005以后才能用
  相关解决方案