删除条件,复杂来看

blueonly 2003-12-19 11:08:35
表ClassTable
字段GoodsId
例GoodsId
1
1
2
2
3
4
_________________
表GoodsTable
字段GoodsId (KEY)

GoodsId
1
2
3
4
5
7
___________________
删除GoodsTalbe中GoodsId不在ClassTable中存在的记录
我想写个过程,可能实现吗?
...全文
35 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueonly 2003-12-19
  • 打赏
  • 举报
回复
我感觉第一种比较直观,总的来说不知道哪个更好
sdhdy 2003-12-19
  • 打赏
  • 举报
回复
delete GoodsTalbe where GoodsId not in(select distinct GoodsId from ClassTable)
victorycyz 2003-12-19
  • 打赏
  • 举报
回复
delete a from goodstable a left join goodsid b on a.goodsid=b.goodsid where b.goodsid is null

最好是goodstable中建一个触发器,goodstable中插入记录时就判断goodsid中有没有相应的记录。 goodsid中建一个删除触发器,同时删除goodstable中的相应的记录。
zjcxc 元老 2003-12-19
  • 打赏
  • 举报
回复

delete GoodsTalbe
from GoodsTalbe a left join GoodsTable b on a.GoodsId=b.GoodsId
where b.GoodsId is null
blueonly 2003-12-19
  • 打赏
  • 举报
回复
第一个就好使,感觉自己没用,啥都的问人,谢谢你门了,给分了
Symsan 2003-12-19
  • 打赏
  • 举报
回复
CREATE PROCEDURE pro_del
AS
delete from GoodsTalbe where GoodsId not in(select GoodsId
from ClassTable)
GO
dlpseeyou 2003-12-19
  • 打赏
  • 举报
回复
delete from GoodsTalbe where GoodsId not in(select GoodsId from ClassTable) a

lynx1111 2003-12-19
  • 打赏
  • 举报
回复
delete from GoodsTalbe where GoodsId not in(select GoodsId
from ClassTable)
pwzhu666 2003-12-19
  • 打赏
  • 举报
回复
delete from GoodsTalbe where GoodsId not in(select GoodsId from ClassTable)
sxsgssgs 2003-12-19
  • 打赏
  • 举报
回复
我修改了一下 Symsan() 的。这下完全正确,我已经试过了。
CREATE PROCEDURE pro_del
AS
delete from GoodsTable where GoodsId not in(select GoodsId
from ClassTable)
GO
yy007 2003-12-19
  • 打赏
  • 举报
回复
刚才我写错了,我把from写成where了!
delete goodstalbe where goodstalbe.goodsid not in(select classtalbe.goodsid from classtalbe)


yy007 2003-12-19
  • 打赏
  • 举报
回复
这个语句我已经测试过了,可以实现你的功能!

delete goodstalbe where goodstalbe.goodsid not in(select classtalbe.goodsid where classtalbe)
gmlxf 2003-12-19
  • 打赏
  • 举报
回复
delete GoodsTalbe where GoodsId not in(select GoodsId
from ClassTable)
qiaolin53 2003-12-19
  • 打赏
  • 举报
回复
delete from GoodsTable where GoodsID not in (select GoodsID from ClassTable)
LoveSQL 2003-12-19
  • 打赏
  • 举报
回复
delete GoodsTalbe where GoodsId not in(select distinct GoodsId from ClassTable)

34,588

社区成员

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

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