比如A表中的PSG_NAME和B表中的RSP_NAME有相同的姓名,去掉A表中的这些行。。。。。
sql语句该怎么写呢?
7 个解决方案
#1
delete from A where psg_name in (select psg_name from b)
#2
delete from A where exists(select 1 from b where a.psg_name =b.psg_name)
跟楼上的语句,查询效率差不多。参考
#3
create table tb(id int,val varchar(10))
insert into tb select 1,'a' union all select 2,'b' union all select 3,'c'
create table tb1(id int,val varchar(10))
insert into tb1 select 2,'a' union all select 4,'b' union all select 6,'c'
go
delete a from tb a inner join tb1 b on a.id=b.id
select * from tb
/*
id val
----------- ----------
1 a
3 c
(2 行受影响)
*/
go
drop table tb,tb1
#4
delete from A where exists(
select 1 from B where a.PSG_NAME=B.RSP_NAME
)
#5
#6
#7
delete from A where exists(select 1 from b where a.psg_name =b.rsp_name)
#1
delete from A where psg_name in (select psg_name from b)
#2
delete from A where exists(select 1 from b where a.psg_name =b.psg_name)
跟楼上的语句,查询效率差不多。参考
#3
create table tb(id int,val varchar(10))
insert into tb select 1,'a' union all select 2,'b' union all select 3,'c'
create table tb1(id int,val varchar(10))
insert into tb1 select 2,'a' union all select 4,'b' union all select 6,'c'
go
delete a from tb a inner join tb1 b on a.id=b.id
select * from tb
/*
id val
----------- ----------
1 a
3 c
(2 行受影响)
*/
go
drop table tb,tb1
#4
delete from A where exists(
select 1 from B where a.PSG_NAME=B.RSP_NAME
)
#5
#6
#7
delete from A where exists(select 1 from b where a.psg_name =b.rsp_name)