才学sql,只知道union可以合并,不知道如何sql语句删除,我现在只实现了数值上的能够相减,不能得到表。请指教!谢谢select count(*) as 常住人口含学生  from 原始 where A102<>2 and A114=1
select count(*) as 学生           from 原始 where A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2

解决方案 »

  1.   

    看标题,这应该是楼主想要的。[code=SQL]1,delete from tab1 where exists(select 1 from tab2 where COL1=tab1.COL1 AND ...)[/code]
      

  2.   

    看不到图片吗?不好意思,不知道什么原因,简单的说就是对一个的表的两个查询
    select count(*) as 常住人口含学生  from 原始 where A102<>2 and A114=1
    select count(*) as 学生           from 原始 where A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2
    第一个查询包含第二个查询(因为第二个查询只是在第一个查询的基础上加了一些限制条件),我想得到一张新表,效果就是就是在第一个查询的基础上去掉第二个查询出来的行,sql语句的union可以实现两个表的相加,但是没有语句可以实现相减。想请高手指教一下。
      

  3.   

    --sql2005/2008,except(第一个集合中存在,但是不存在于第二个集合中的数据);
                   INTERSECT(两个集合中都存在的数据)
    --在2000中没有,要用not eexists
      

  4.   

    谢谢指导,但是还是有问题,我写了代码,查询结果是空
    select * from 调查表 where A102<>2 and A114=1 and not exists(select * from 调查表 where A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2)
      

  5.   

    select * from 调查表 a where A102<>2 and A114=1 and not exists(select * from 调查表 a where a.主键字段=b.主键字段 and A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2)
    --或者
    select * from 调查表  where A102<>2 and A114=1 
    and  主键字段 not in 
    (select 主键字段 from 调查表  where a.主键字段=b.主键字段 and A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2)
      

  6.   

    --上面的或者错了,应该是这样
    select * from 调查表  where A102<>2 and A114=1 
    and  主键字段 not in 
    (select 主键字段 from 调查表  where A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2)
      

  7.   

    谢谢楼上的回答,但是我有两个问题还是想问一下
    1、用exists,为什么会有调查表a,b
    2、为什么要用主键,假如我这个表没有设置主键或者是双主键,又怎么实现呢
    再次表示感谢!
      

  8.   

    第一个也写错了,应该把a改为b,多个字段组合主键的化,如下:select * from 调查表 a where A102<>2 and A114=1 
    and not exists(select * from 调查表 b 
    where a.主键字段1=b.主键字段1 and  a.主键字段2=b.主键字段2...
    and A102<>2 and A114=1 and A117=1 and A118>=5 and A113<2)ps:上下两个表必须关联进行比对,所以这样
      

  9.   

    --算了,楼主看下面这个写法吧
    select count(*) as 常住人口含学生  
    from 原始 where A102<>2 and A114=1
    and not (A117=1 and A118>=5 and A113<2)
      

  10.   

    感谢CD731107,就是这个意思,我不知道还可以这样写 not (A117=1 and A118>=5 and A113<2),一开始想了很多办法,什么逻辑运算的摩根律和分配率啊,越写越麻烦,可以这样写就好了,十分感谢cd
      

  11.   

    delete from exam372 where exam372.name in (select name from exam372_TEMP b )
    delete from exam372 a where EXISTS (select name from exam372_TEMP b where a.name=b.name)