queue表
queueid  queuename
1000        abc
2000         cds
3000        sdff
4000        s3443CallerQueue表
QueueId   Callerid
1000       1
2000       2
3000       3
5000       4如上表,要求删除callerqueue表里面queueid在queue表里面不存在的记录(如上表应删除queueid=5000这条记录),这个sql语句怎么写?

解决方案 »

  1.   

    delete CallerQueue 
    where QueueId  not in (select queueid   from queue )
      

  2.   

    DELETE FROM CallerQueue 
    FROM CallerQueue T1
    WHERE NOT EXISTS(SELECT 1 FROM queue T2 WHERE T2.QueueId=T1.QueueId)
      

  3.   

    delete from CallerQueue where  QueueId  not in (select QueueId from queue)
      

  4.   

    DELETE callerqueue FROM 
    callerqueue C WHERE NOT EXISTS(SELECT 1 FROM queue WHERE queueid=C.queueid)
      

  5.   

    delete a 
    from CallerQueu a
    where not exists(select * from [queue] where  queueid<>a.queueid)
      

  6.   

    或者这样写
    DELETE FROM CallerQueue 
    WHERE QueueId<>ANY(SELECT queueid   from queue)或者像1L那样
      

  7.   

    CheckSum() 函数  查找A表中有而B表中没有的数据行
      

  8.   

    DELETE FROM CallerQueue 
    FROM CallerQueue T1
    WHERE NOT EXISTS(SELECT 1 FROM queue T2 WHERE T2.QueueId=T1.QueueId)
    多了个FROM
      

  9.   

    有ID就不用CHECKSUM了,如果是全字段匹配用CHECKSUM不错
      

  10.   

    if object_id('[queue]') is not null drop table [queue]
    create table [queue]
    (
    queueid char(10),
    queuename char(10)
    )insert [queue]
    select '1000','abc' union all 
    select '2000','cds' union all 
    select '3000','sdff' union all 
    select '4000','s344'if object_id('[CallerQueue]') is not null drop table [CallerQueue]
    create table [CallerQueue]
    (
    queueid char(10),
    Callerid int
    )insert [CallerQueue]
    select '1000',1 union all 
    select '2000',2 union all 
    select '3000',3 union all 
    select '5000',4delete  from  [CallerQueue]
    where checksum(queueid)not in (select checksum(queueid)from  [queue])select * from   [CallerQueue]