| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1214 人关注过本帖
标题:判断请教
只看楼主 加入收藏
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
等 级:版主
威 望:191
帖 子:3146
专家分:8408
注 册: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
快速回复:判断请教
数据加载中...
 
   



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

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