如何用SQL语句实现在一个表中删除掉另一个表中相同的数据行

mark0704 2011-08-05 02:56:14
才学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
...全文
2055 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
developerjim 2012-02-16
  • 打赏
  • 举报
回复
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)
mark0704 2011-08-05
  • 打赏
  • 举报
回复
然后又想了两个查询相减,真是自己把自己复杂了。
mark0704 2011-08-05
  • 打赏
  • 举报
回复
感谢CD731107,就是这个意思,我不知道还可以这样写 not (A117=1 and A118>=5 and A113<2),一开始想了很多办法,什么逻辑运算的摩根律和分配率啊,越写越麻烦,可以这样写就好了,十分感谢cd
cd731107 2011-08-05
  • 打赏
  • 举报
回复
--算了,楼主看下面这个写法吧
select count(*) as 常住人口含学生
from 原始 where A102<>2 and A114=1
and not (A117=1 and A118>=5 and A113<2)
cd731107 2011-08-05
  • 打赏
  • 举报
回复
第一个也写错了,应该把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:上下两个表必须关联进行比对,所以这样


mark0704 2011-08-05
  • 打赏
  • 举报
回复
谢谢楼上的回答,但是我有两个问题还是想问一下
1、用exists,为什么会有调查表a,b
2、为什么要用主键,假如我这个表没有设置主键或者是双主键,又怎么实现呢
再次表示感谢!
cd731107 2011-08-05
  • 打赏
  • 举报
回复
--上面的或者错了,应该是这样
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)
AcHerat 元老 2011-08-05
  • 打赏
  • 举报
回复
楼主你说说要删除的是哪个表,根据哪个表,删除的条件是什么?
cd731107 2011-08-05
  • 打赏
  • 举报
回复
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)
mark0704 2011-08-05
  • 打赏
  • 举报
回复
谢谢指导,但是还是有问题,我写了代码,查询结果是空
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)
Billy 2011-08-05
  • 打赏
  • 举报
回复
--sql2005/2008,except(第一个集合中存在,但是不存在于第二个集合中的数据);
INTERSECT(两个集合中都存在的数据)
--在2000中没有,要用not eexists
mark0704 2011-08-05
  • 打赏
  • 举报
回复
看不到图片吗?不好意思,不知道什么原因,简单的说就是对一个的表的两个查询
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可以实现两个表的相加,但是没有语句可以实现相减。想请高手指教一下。
快溜 2011-08-05
  • 打赏
  • 举报
回复
挂了挂了挂了。
一缕青烟 2011-08-05
  • 打赏
  • 举报
回复
看标题,这应该是楼主想要的。

[code=SQL]

1,delete from tab1 where exists(select 1 from tab2 where COL1=tab1.COL1 AND ...)



[/code]
AcHerat 元老 2011-08-05
  • 打赏
  • 举报
回复
楼主的问题值得我们深思!
--小F-- 2011-08-05
  • 打赏
  • 举报
回复
需要得到什么结果??什么删除??图片挂了

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧