日期:2014-05-17  浏览次数:20577 次

请教简单问题
原表有两个

id 姓名
1 刘大
2 关二
3 张三
4 李四
5 王五



id 姓名
1 刘大
2 唐僧
3 孙悟空
4 李四
5 王五

想得到表


id 姓名 是否一致
1 刘大 y
2 关二 n
3 张三 n
4 李四 y
5 王五 y



id 姓名 是否一致
1 刘大 y
2 唐僧 n
3 孙悟空 n
4 李四 y
5 王五 y

请教简单的方法,谢谢!!

------解决方案--------------------

create table t1
(
id int,
姓名 nvarchar(3)
)
insert into t1 values(1,'刘大')
insert into t1 values(2,'关二')
insert into t1 values(3,'张三')
insert into t1 values(4,'李四')
insert into t1 values(5,'王五')


create table t2
(
id int,
姓名 nvarchar(3)
)
insert into t2 values(1,'刘大')
insert into t2 values(2,'唐僧')
insert into t2 values(3,'孙悟空')
insert into t2 values(4,'李四')
insert into t2 values(5,'王五')

select t1.id,t1.姓名, case when t1.姓名 = t2.姓名 then 'y' else 'n' end from t1,t2
where t1.id = t2.id

/*
id,姓名,
1,刘大,y
2,关二,n
3,张三,n
4,李四,y
5,王五,y

(5 行受影响)
*/
select t2.id,t2.姓名, case when t1.姓名 = t2.姓名 then 'y' else 'n' end from t1,t2
where t1.id = t2.id
/*
id,姓名,
1,刘大,y
2,唐僧,n
3,孙悟空,n
4,李四,y
5,王五,y

(5 行受影响)