问题应该简单,好了,立即给分,(星期三早上结帖)有表user_info
id uname turename
1 hf001 张三
2 hf002 李四
3 nc005 张一
4 fz007 李二
.....
n ty157 李五
另一张表user_plpl_id uname title
1 hf001 其它内容1
2 fz007 其它内容2
3 nc005 其它内容3
4 ping 其它内容4
表user_pl中的字段uname部分来自表user_info中的uname
现在我想把表user_pl中的字段uname如果是表一里面的改成用表一中的id号表示,
不是表一的字段就不动,应该怎么查询呀,表一的uname 字段具有唯一性
结果表应该是这样的
pl_id uname title
1 1 其它内容1
2 4 其它内容2
3 3 其它内容3
4 ping 其它内容4
id uname turename
1 hf001 张三
2 hf002 李四
3 nc005 张一
4 fz007 李二
.....
n ty157 李五
另一张表user_plpl_id uname title
1 hf001 其它内容1
2 fz007 其它内容2
3 nc005 其它内容3
4 ping 其它内容4
表user_pl中的字段uname部分来自表user_info中的uname
现在我想把表user_pl中的字段uname如果是表一里面的改成用表一中的id号表示,
不是表一的字段就不动,应该怎么查询呀,表一的uname 字段具有唯一性
结果表应该是这样的
pl_id uname title
1 1 其它内容1
2 4 其它内容2
3 3 其它内容3
4 ping 其它内容4
from user_pl a left outer join user_info b
on a.uname=b.uname
go
create table user_info(id int,uname varchar(10),turename varchar(10))
insert into user_info
select 1,'hf001','张三'
union all select 2,'hf002','李四'
union all select 3,'nc005','张一'
union all select 4,'fz007','李二'create table user_pl(pl_id int,uname varchar(10),title varchar(20))
insert into user_pl
select 1,'hf001','其它内容1'
union all select 2,'fz007','其它内容2'
union all select 3,'nc005','其它内容3'
union all select 4,'ping','其它内容4'select a.pl_id,isnull(rtrim(b.id),a.uname) as uname,a.title
from user_pl a
left join user_info b on a.uname=b.uname
/*
pl_id uname title
----------- ------------ --------------------
1 1 其它内容1
2 4 其它内容2
3 3 其它内容3
4 ping 其它内容4(所影响的行数为 4 行)
*/
select 1,'hf001','张三' union all
select 2,'hf002','李四' union all
select 3,'nc005','张一' union all
select 4,'fz007','李二' create table user_pl(pl_id int,uname char(6),title char(30))insert into user_pl
select 1,'hf001','其他内容1' union all
select 2,'fz007','其他内容2' union all
select 3,'nc005','其他内容3' union all
select 4,'ping' ,'其他内容4'update user_plset user_pl.uname=convert(char(6) ,user_info.id) from user_info ,user_pl
where user_info.uname=user_pl.unameselect * from user_info
select * from user_pldrop table user_info,user_pl
go
create table user_info(id int,uname varchar(10),turename varchar(10))
insert into user_info
select 1,'hf001','张三'
union all select 2,'hf002','李四'
union all select 3,'nc005','张一'
union all select 4,'fz007','李二'create table user_pl(pl_id int,uname varchar(10),title varchar(20))
insert into user_pl
select 1,'hf001','其它内容1'
union all select 2,'fz007','其它内容2'
union all select 3,'nc005','其它内容3'
union all select 4,'ping','其它内容4'select a.pl_id , isnull(b.id,a.uname) as uname ,a.title
from user_pl a left outer join user_info b
on a.uname=b.uname
/*
服务器: 消息 245,级别 16,状态 1,行 1
将 varchar 值 'ping' 转换为数据类型为 int 的列时发生语法错误。
*/