表1:
代码 编号 机构
000001 0001 01
000001 0011 02
000001 0019 05
000002 0001 01
000002 0011 03
000003 0002 02
000003 0022 01
000004 0013 01
000004 0019 01
000005 0001 02
000005 0002 02
000010 0001 01
000010 0090 01表2:
编号 标志
0001 1
0002 1
0003 1
0011 2
0013 2
0019 3
0022 3想从表1得到
1.编号在表2中存在,并且踢除编号在表2中标志等于3的该代码所有纪录.
2.编号在表2中存在,并且踢除编号在表2中标志等于2和3的该代码所有纪录.正确结果是:
1.
000002 0001 01
000002 0011 03
000005 0001 02
000005 0002 02
000010 0001 01
2.
000005 0001 02
000005 0002 02
000010 0001 01
代码 编号 机构
000001 0001 01
000001 0011 02
000001 0019 05
000002 0001 01
000002 0011 03
000003 0002 02
000003 0022 01
000004 0013 01
000004 0019 01
000005 0001 02
000005 0002 02
000010 0001 01
000010 0090 01表2:
编号 标志
0001 1
0002 1
0003 1
0011 2
0013 2
0019 3
0022 3想从表1得到
1.编号在表2中存在,并且踢除编号在表2中标志等于3的该代码所有纪录.
2.编号在表2中存在,并且踢除编号在表2中标志等于2和3的该代码所有纪录.正确结果是:
1.
000002 0001 01
000002 0011 03
000005 0001 02
000005 0002 02
000010 0001 01
2.
000005 0001 02
000005 0002 02
000010 0001 01
select t1.代码,t1.编号,t1.机构
from t1 join t2 on t1.编号 = t2.编号
where t2.标志 <> 2/*****************************************/select t1.代码,t1.编号,t1.机构
from t1 join t2 on t1.编号 = t2.编号
where t2.标志 <> 2 or t2.标志 <> 3
select t1.* from t1 where 编号 not in (select 编号 from t2 where 标志 = 3) and 编号 in (select 编号 from t2)2.编号在表2中存在,并且踢除编号在表2中标志等于2和3的该代码所有纪录.
select t1.* from t1 where 编号 not in (select 编号 from t2 where 标志 = 3 or 标志 = 2) and 编号 in (select 编号 from t2)
insert into tb1 values('000001', '0001', '01')
insert into tb1 values('000001', '0011', '02')
insert into tb1 values('000001', '0019', '05')
insert into tb1 values('000002', '0001', '01')
insert into tb1 values('000002', '0011', '03')
insert into tb1 values('000003', '0002', '02')
insert into tb1 values('000003', '0022', '01')
insert into tb1 values('000004', '0013', '01')
insert into tb1 values('000004', '0019', '01')
insert into tb1 values('000005', '0001', '02')
insert into tb1 values('000005', '0002', '02')
insert into tb1 values('000010', '0001', '01')
insert into tb1 values('000010', '0090', '01')
create table tb2(编号 varchar(10),标志 int)
insert into tb2 values('0001', 1)
insert into tb2 values('0002', 1)
insert into tb2 values('0003', 1)
insert into tb2 values('0011', 2)
insert into tb2 values('0013', 2)
insert into tb2 values('0019', 3)
insert into tb2 values('0022', 3)
go
--1.编号在表2中存在,并且踢除编号在表2中标志等于3的该代码所有纪录.
select * from tb1 where 代码 not in(
select distinct m.代码 from tb1 m, tb2 n where m.编号 = n.编号 and n.标志 = 3)
/*
代码 编号 机构
---------- ---------- ----------
000002 0001 01
000002 0011 03
000005 0001 02
000005 0002 02
000010 0001 01
000010 0090 01(所影响的行数为 6 行)
*/--2.编号在表2中存在,并且踢除编号在表2中标志等于2和3的该代码所有纪录.
select * from tb1 where 代码 not in(
select distinct m.代码 from tb1 m, tb2 n where m.编号 = n.编号 and (n.标志 = 3 or n.标志 = 2))
/*
代码 编号 机构
---------- ---------- ----------
000005 0001 02
000005 0002 02
000010 0001 01
000010 0090 01(所影响的行数为 4 行)
*/drop table tb1 , tb2
from t1 t
where exists(select 1 from t2 where 编号=t.编号)
and not exists(select 1 from t1 where t1.代码=t.代码
and exists(select 1 from t2 where 编号=t1.编号 and 标志=3)
)/**
代码 编号 机构
------ ---- ----
000002 0001 01
000002 0011 03
000005 0001 02
000005 0002 02
000010 0001 01(5 行受影响)**/select *
from t1 t
where exists(select 1 from t2 where 编号=t.编号)
and not exists(select 1 from t1 where t1.代码=t.代码
and exists(select 1 from t2 where 编号=t1.编号 and 标志 in(2,3))
)
/**
代码 编号 机构
------ ---- ----
000005 0001 02
000005 0002 02
000010 0001 01(3 行受影响)**/
insert into tb1 values('000001', '0001', '01')
insert into tb1 values('000001', '0011', '02')
insert into tb1 values('000001', '0019', '05')
insert into tb1 values('000002', '0001', '01')
insert into tb1 values('000002', '0011', '03')
insert into tb1 values('000003', '0002', '02')
insert into tb1 values('000003', '0022', '01')
insert into tb1 values('000004', '0013', '01')
insert into tb1 values('000004', '0019', '01')
insert into tb1 values('000005', '0001', '02')
insert into tb1 values('000005', '0002', '02')
insert into tb1 values('000010', '0001', '01')
insert into tb1 values('000010', '0090', '01')
create table tb2(编号 varchar(10),标志 int)
insert into tb2 values('0001', 1)
insert into tb2 values('0002', 1)
insert into tb2 values('0003', 1)
insert into tb2 values('0011', 2)
insert into tb2 values('0013', 2)
insert into tb2 values('0019', 3)
insert into tb2 values('0022', 3)
go
--1.编号在表2中存在,并且踢除编号在表2中标志等于3的该代码所有纪录.
select * from tb1 where 代码 not in(
select distinct m.代码 from tb1 m, tb2 n where m.编号 = n.编号 and n.标志 = 3)
/*
代码 编号 机构
---------- ---------- ----------
000002 0001 01
000002 0011 03
000005 0001 02
000005 0002 02
000010 0001 01
000010 0090 01(所影响的行数为 6 行)
*/select * from tb1 where 代码 not in(
select distinct m.代码 from tb1 m, tb2 n where m.编号 = n.编号 and n.标志 = 3)
and 编号 in (select 编号 from tb2)
/*
代码 编号 机构
---------- ---------- ----------
000002 0001 01
000002 0011 03
000005 0001 02
000005 0002 02
000010 0001 01(所影响的行数为 5 行)
*/--2.编号在表2中存在,并且踢除编号在表2中标志等于2和3的该代码所有纪录.
select * from tb1 where 代码 not in(
select distinct m.代码 from tb1 m, tb2 n where m.编号 = n.编号 and (n.标志 = 3 or n.标志 = 2))
/*
代码 编号 机构
---------- ---------- ----------
000005 0001 02
000005 0002 02
000010 0001 01
000010 0090 01(所影响的行数为 4 行)
*/select * from tb1 where 代码 not in(
select distinct m.代码 from tb1 m, tb2 n where m.编号 = n.编号 and (n.标志 = 3 or n.标志 = 2))
and 编号 in (select 编号 from tb2)
/*
代码 编号 机构
---------- ---------- ----------
000005 0001 02
000005 0002 02
000010 0001 01(所影响的行数为 3 行)
*/drop table tb1 , tb2
受大家的启发我是这样的"
select t1.*
from t1
where 代码 not in
(select 代码 from t1 where 编号 in (select 编号 from t2 where 标志 = 3))
and 编号 in (select 编号 from t2)但由于实际工作中t1纪录非常多(数千万条)不知那种效率高(t2纪录少)?