日期:2014-05-17  浏览次数:20393 次

求不存在记录
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 
  (
  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 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以后才能用
------其他解决方案--------------------
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
  where xxx='xxx'
  order by NEWID()

------其他解决方案--------------------
引用:
select top 20 *  from A表  
except
select * from B表
order by newid();

如果A表后面有where 条件的话 这个SQL怎么写?
------其他解决方案--------------------
引用:
SQL code1234567  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()

谢谢了 吃完午饭结贴