标题:判断请教
只看楼主
ppppooiiuu
Rank: 1
等 级:新手上路
帖 子:158
专家分:0
注 册:2016-3-2
结帖率:46.15%
已结贴  问题点数:20 回复次数:1 
判断请教
假设有两个表,表1有一列,表2也有一列,如何判断表1的值是否有在表2
列   列  是否
a    b   否
b    c   是
c        是
d        否
搜索更多相关主题的帖子: 如何 
2017-03-08 17:04
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:190
帖 子:3125
专家分:8340
注 册:2015-3-25
得分:20 
if object_id('tempdb..#表1') is not null drop table #表1
create table #表1(列1 varchar(10),列2 varchar(10))
insert into #表1
select '101','201' union all
select '102','202' union all
select '103','203' union all
select '104','204' union all
select '105','205' union all
select '106','206'


if object_id('tempdb..#表2') is not null drop table #表2
create table #表2(列1 varchar(10),列2 varchar(10))
insert into #表2
select '109','209' union all
select '107','207' union all
select '105','205' union all
select '103','203' union all
select '101','201' union all
select '100','200'

--查表1在表2是否存在
select a.列1,a.列2,case when a.列1=b.列1 then '是' else '否' end as 是否 from #表1 a
left join #表2 b on a.列1=b.列1

--查表2在表1是否存在
select b.列1,b.列2,case when a.列1=b.列1 then '是' else '否' end as 是否 from #表2 b
left join #表1 a on a.列1=b.列1

--只查2个表都有的
select a.列1,a.列2,case when a.列1=b.列1 then '是' else '否' end as 是否 from #表1 a
inner join #表2 b on a.列1=b.列1


2017-03-09 10:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-474865-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.082918 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved