关于一个Sql语句,各位来看看

studyforme 2005-10-25 06:16:19
两个表结构为:
Ta
k1 k2 e3(字段名称)
1 1 1
1 2 3
d 3 f
sdf 1 1

Tb
k1 k2 f3
1 1 1
1 2 3
f f f
d d s
sdf sdf s

我要的结果集为:
f f f
d d s
sdf sdf s
结果集从Tb中选出.要求是:

选出来的结果中记录的 k1 和 k2 值组合不能在Ta中出现。
如:Tb中记录 1 1 1 ,因为k1和k2的组合为1 1,在Ta中出现过,故不能选择,各位,帮帮我想一条Sql语句实现上述选择,谢谢

...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ls_jingwen 2005-10-25
  • 打赏
  • 举报
回复
declare @Ta table(k1 varchar(10),k2 varchar(10),e3 varchar(10))
insert into @Ta select '1','1','1'
union all select '1','2','3'
union all select 'd','3','f'
union all select 'sdf','1','1'


declare @Tb table(k1 varchar(10),k2 varchar(10),f3 varchar(10))
insert into @Tb select '1','1','1'
union all select '1','2','3'
union all select 'f','f','f'
union all select 'd','d','s'
union all select 'sdf','sdf','s'

select * from @tb b where not exists(select 1 from @ta a where a.k1=b.k1 and a.k2=b.k2 )

結果:
k1 k2 f3
---------- ---------- ----------
f f f
d d s
sdf sdf s
$扫地僧$ 2005-10-25
  • 打赏
  • 举报
回复
declare @Ta table(k1 varchar(10),k2 varchar(10),e3 varchar(10))
insert into @Ta select '1','1','1'
union all select '1','2','3'
union all select 'd','3','f'
union all select 'sdf','1','1'


declare @Tb table(k1 varchar(10),k2 varchar(10),f3 varchar(10))
insert into @Tb select '1','1','1'
union all select '1','2','3'
union all select 'f','f','f'
union all select 'd','d','s'
union all select 'sdf','sdf','s'

select * from @tb where (cast(k1 as varchar) +cast(k2 as varchar)) not in (select (cast(k1 as varchar) +cast(k2 as varchar)) from @ta)
zlp321002 2005-10-25
  • 打赏
  • 举报
回复
判断是否有记录
studyforme 2005-10-25
  • 打赏
  • 举报
回复
请问
select 1 是什么意思?
zlp321002 2005-10-25
  • 打赏
  • 举报
回复
--测试环境
declare @Ta table(k1 varchar(10),k2 varchar(10),e3 varchar(10))
insert into @Ta select '1','1','1'
union all select '1','2','3'
union all select 'd','3','f'
union all select 'sdf','1','1'


declare @Tb table(k1 varchar(10),k2 varchar(10),f3 varchar(10))
insert into @Tb select '1','1','1'
union all select '1','2','3'
union all select 'f','f','f'
union all select 'd','d','s'
union all select 'sdf','sdf','s'

--查询
select * from @Tb B
where not exists (select 1 from @Ta where k1=B.K1 and K2=B.K2 and e3=B.f3)
--结果
k1 k2 f3
---------- ---------- ----------
f f f
d d s
sdf sdf s

(所影响的行数为 3 行)
zlp321002 2005-10-25
  • 打赏
  • 举报
回复
--测试环境
declare @Ta table(k1 varchar(10),k2 varchar(10),e3 varchar(10))
insert into @Ta select '1','1','1'
union all select '1','2','3'
union all select 'd','3','f'
union all select 'sdf','1','1'


declare @Tb table(k1 varchar(10),k2 varchar(10),f3 varchar(10))
insert into @Tb select '1','1','1'
union all select '1','2','3'
union all select 'f','f','f'
union all select 'd','d','s'
union all select 'sdf','sdf','s'

--查询
select * from @Tb B
where not exists (select 1 from @Ta where k1=B.K1 and K2=B.K2 and e3=B.f3)
--结果
k1 k2 f3
---------- ---------- ----------
f f f
d d s
sdf sdf s

(所影响的行数为 3 行)

zlp321002 2005-10-25
  • 打赏
  • 举报
回复
--try
select * from Tb B
where not exists (select 1 from Ta where k1=B.K1 and K2=B.K2 and e3=B.e3)

34,873

社区成员

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

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