简单问题(=及not in),高分重谢

insert2003 2007-06-07 11:48:34
有两个表
tb_test1
a1 a2
1 a
1 b
1 c

tb_test2
b1 b2
1 a
1 b
1 d
2 c
条件:
a1=b1且a2 not in b2
得到的结果应该是
tb_test1中的 1 c
...全文
284 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
肥胖的柠檬 2007-06-07
  • 打赏
  • 举报
回复
select a.* from tb_test1 a not exists(select 1 from tb_test2 where b1=a.a1 and b2=a.a2)
select a.* from tb_test1 a left join tb_test2 b on a.a1=b.b1 and a.a2=b.b2
where b.b1 is null

只支持这2种好方法~HOHO
ojuju10 2007-06-07
  • 打赏
  • 举报
回复

select * from tb_test1 a
where not exists(select 1 from tb_test2 where a.a1=b1 and a.a2=b2 )
bill024 2007-06-07
  • 打赏
  • 举报
回复
select distinct a.a1,a.a2 from tb_test1 a inner join tb_test2 b
on a.a1=b.b1
where a.a2 not in
(
select b2 from tb_test2 where b1=b.b1
)
Andy-W 2007-06-07
  • 打赏
  • 举报
回复
来晚几步,呵呵
吃饭了
ojuju10 2007-06-07
  • 打赏
  • 举报
回复
select * from tb_test1 a
where exists(select 1 from tb_test2 where a.a1=b1 )
and not exists(select 1 from tb_test2 where a.a2=b2 )
Andy-W 2007-06-07
  • 打赏
  • 举报
回复
CREATE TABLE tb_test1(a1 int,a2 nvarchar(2))
INSERT INTO tb_test1
SELECT 1 ,'a' UNION ALL
SELECT 1 ,'b' UNION ALL
SELECT 1 ,'c'
CREATE TABLE tb_test2(b1 int,b2 nvarchar(2))
INSERT INTO tb_test2
SELECT 1 ,'a' UNION ALL
SELECT 1 ,'b' UNION ALL
SELECT 1 ,'d' UNION ALL
SELECT 2 ,'c'

SELECT * FROM tb_test1 AS A WHERE A.a2 NOT IN(SELECT b2 FROM tb_test2 AS B WHERE A.a1=B.b1)

DROP TABLE tb_test1,tb_test2
wgzaaa 2007-06-07
  • 打赏
  • 举报
回复
select a.* from tb_test1 a,tb_test2 b where a1=b1 and a2<>b2
肥胖的柠檬 2007-06-07
  • 打赏
  • 举报
回复
zsforever(虎虎) ( ) 信誉:100 Blog 加为好友 2007-06-07 11:54:33 得分: 0


another way:
select a.* from tb_test1 a left join tb_test2 b on a.a1=b.b1 and a.a2=b.b2
where b.b1 is null


好方法~
lwl0606 2007-06-07
  • 打赏
  • 举报
回复
create table tb_test1 (a1 int , a2 nvarchar(10))
insert into tb_test1 select 1 , 'a'
union select 1 , 'b'
union select 1 , 'c'

create table tb_test2(b1 int , b2 nvarchar(10))
insert into tb_test2
select 1 , 'a'
union select 1 , 'b'
union select 1 , 'd'
union select 2 , 'c'


select distinct a.*
from tb_test1 a left outer join tb_test2 b
on a.a1=b.b1
where a2 not in (select b2 from tb_test2 where a.a1=tb_test2.b1)

--result
1 c
zsforever 2007-06-07
  • 打赏
  • 举报
回复
another way:
select a.* from tb_test1 a left join tb_test2 b on a.a1=b.b1 and a.a2=b.b2
where b.b1 is null
子陌红尘 2007-06-07
  • 打赏
  • 举报
回复
declare @tb_test1 table(a1 int,a2 varchar(4))
insert into @tb_test1 select 1,'a'
insert into @tb_test1 select 1,'b'
insert into @tb_test1 select 1,'c'

declare @tb_test2 table(b1 int,b2 varchar(4))
insert into @tb_test2 select 1,'a'
insert into @tb_test2 select 1,'b'
insert into @tb_test2 select 1,'d'
insert into @tb_test2 select 2,'c'

select
a.*
from
@tb_test1 a
where
exists(select 1 from @tb_test2 where b1=a.a1)
and
not exists(select 1 from @tb_test2 where b1=a.a1 and b2=a.a2)

/*
a1 a2
----------- ----
1 c
*/
lwl0606 2007-06-07
  • 打赏
  • 举报
回复

select a.*
from tb_test1 a left outer join tb_test2 b
on a.a1=b.b1 and a.a2<.b.b2
子陌红尘 2007-06-07
  • 打赏
  • 举报
回复
select a.* from tb_test1 a not exists(select 1 from tb_test2 where b1=a.a1 and b2=a.a2)
fa_ge 2007-06-07
  • 打赏
  • 举报
回复

34,837

社区成员

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

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