注册 登录
编程论坛 SQL Server论坛

问一个数据库查询的问题

小糊涂仙 发布于 2007-09-07 03:24, 846 次点击

表一:
资产 登记人 使用人 管理人
电脑 1 5 6


表二:
编号 姓名
1 猪八戒
2 孙悟空
3 唐僧
4 如来
5 观音
6 王母

请问我要查询表一,让1,5,6显示出对应的姓名,应该怎么查询?查询语句该怎么写呢?

6 回复
#2
卡卡艾2007-09-07 08:38
没看懂
#3
缘吇弹2007-09-07 13:33
Me 2
#4
purana2007-09-07 13:36
select
(select 姓名 from t2 where t1.登记人=t2.编号) As Name1 ,
(select 姓名 from t2 where t1.使用人=t2.编号) As Name2 ,
(select 姓名 from t2 where t1.管理人=t2.编号) As Name3
from t1
#5
bygg2007-09-07 13:44
这里有个笨方法,但可以实现LZ想要的效果

select 资产,
(case 登记人 when (select 编号 from 表二 where 编号=登记人) then (select 姓名 from 表二 where 编号=登记人) end) as 登记人,
(case 使用人 when (select 编号 from 表二 where 编号=使用人) then (select 姓名 from 表二 where 编号=使用人) end) as 使用人,
(case 管理人 when (select 编号 from 表二 where 编号=管理人) then (select 姓名 from 表二 where 编号=管理人) end) as 管理人
from 表一

用存储过程/方法更好

[此贴子已经被作者于2007-9-7 13:49:58编辑过]

#6
thllilac2007-09-07 14:24
select
资产,
(select 姓名 from test4 where test3.登记人=test4.编号) as 登记人 ,
(select 姓名 from test4 where test3.使用人=test4.编号) as 使用人,
(select 姓名 from test4 where test3.管理人=test4.编号) as 管理人
from test3
#7
小糊涂仙2007-09-07 19:08
OY
收到!!
谢谢
1