如何删除三表关联数据?

DTWUJP 2009-11-01 09:51:10
如A表
id name date flag
01 a 2009-01-01 4
02 b 2009-05-01 1
03 c 2009-05-01 4
04 d 2009-07-01 1
......
b表
id xmid xmname fair
01 a01 aaaa 300
01 a02 bbbbb 40
01 a03 ccccc 10
02 a07 dddd 0
02 a06 eeee 0
02 a14 ffff 30
03 a25 g 56
04 a25 g 23
.........
C表
xmid xm price count
a01 1 6 2
a01 2 5 6
a01 3 3 54
a02 1 4 10

.....

a表通过id和B表关联,a表和b表是一对多关系,b表和c表通过xmid关联,b表和c表是多对多关系,
请问,如何将a表中日期在‘2009-06-01’以前和flag=4的b表和c表数据删除,但保留a表中的数据?
...全文
252 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixianxxx 2009-11-01
  • 打赏
  • 举报
回复
create table #A
(
id varchar(10),
name varchar(20),
date datetime,
flag int
)
insert into #A select '01','a','2009-01-01',4
union all select '02','b','2009-05-01',1
union all select '03','c','2009-05-01',4
union all select '04','d','2009-07-01',1
create table #B1
(
id varchar(10),
xmid varchar(10),
xmname nvarchar(20),
fair int
)
insert into #B1 select '01','a01','aaaa',300
union all select '01','a02','bbbbb',40
union all select '01','a03','ccccc',10
union all select '02','a07','dddd',0
union all select '02','a06','eeee',0
union all select '02','a14','ffff',30
union all select '03','a25','g',56
union all select '04','a25','g',23
create table #C1
(
xmid varchar(10),
xm int,
price int,
count int
)
insert into #C1 select 'a01',1,6,2
union all select 'a01',2,5,6
union all select 'a01',3,3,54
union all select 'a02',1,4,10
GO
CREATE PROC DEL_A
AS
BEGIN
--先要删除#c1
delete #C1
from #a a join #b1 b on a.id=b.id
JOIN #C1 C ON B.xmid=C.xmid
where [date]<'2009-06-01' and flag=4
--然后删除关联的#B1
delete #b1
from #a a join #b1 b on a.id=b.id
where [date]<'2009-06-01' and flag=4
END
--
exec dbo.DEL_A
--检测--
SELECT * FROM #B1
SELECT * FROM #C1
/*
id xmid xmname fair
---------- ---------- -------------------- -----------
02 a07 dddd 0
02 a06 eeee 0
02 a14 ffff 30
04 a25 g 23

(4 行受影响)

xmid xm price count
---------- ----------- ----------- -----------
*/

果果数据用下
lidanzi 2009-11-01
  • 打赏
  • 举报
回复
学习来的
feixianxxx 2009-11-01
  • 打赏
  • 举报
回复
......
ws_hgo 2009-11-01
  • 打赏
  • 举报
回复
create table #A
(
id varchar(10),
name varchar(20),
date datetime,
flag int
)
insert into #A select '01','a','2009-01-01',4
union all select '02','b','2009-05-01',1
union all select '03','c','2009-05-01',4
union all select '04','d','2009-07-01',1
create table #B1
(
id varchar(10),
xmid varchar(10),
xmname nvarchar(20),
fair int
)
insert into #B1 select '01','a01','aaaa',300
union all select '01','a02','bbbbb',40
union all select '01','a03','ccccc',10
union all select '02','a07','dddd',0
union all select '02','a06','eeee',0
union all select '02','a14','ffff',30
union all select '03','a25','g',56
union all select '04','a25','g',23
create table #C1
(
xmid varchar(10),
xm int,
price int,
count int
)
insert into #C1 select 'a01',1,6,2
union all select 'a01',2,5,6
union all select 'a01',3,3,54
union all select 'a02',1,4,10
/*
表通过id和B表关联,a表和b表是一对多关系,b表和c表通过xmid关联,b表和c表是多对多关系,
请问,如何将a表中日期在‘2009-06-01’以前和flag=4的b表和c表数据删除,但保留a表中的数据?
*/

--删除C表的
delete from #C1 where xmid in
(
select xmid from #B1 where id in (select id from #A where date<'2009-06-01' and flag=4)
)
--删除B表
delete from #B1 where id in (select id from #A where date<'2009-06-01' and flag=4)
SQL77 2009-11-01
  • 打赏
  • 举报
回复
建立外键关联起来呀,用级联方便

要不写个触发器
xman_78tom 2009-11-01
  • 打赏
  • 举报
回复

delete C where xmid in
(select xmid from B where id in (select id from A where [date]<'2009-06-01' and flag=4))

delete B where id in (select id from A where [date]<'2009-06-01' and flag=4)

DTWUJP 2009-11-01
  • 打赏
  • 举报
回复
谢谢楼上,表中没有外键,能给个具体语句吗?
guguda2008 2009-11-01
  • 打赏
  • 举报
回复
级联删除或者存储过程

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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