50 求一条 SQL 语句,谢谢
有两张表 A , B
A:
ID TYPE DETAIL
101 类型1 类型101详细
201 类型2 类型201详细
103 类型1 类型103详细
104 类型1 类型104详细
B:
ID_1 ID_2
101 201
102 202
103 203
...
想实现 select detail_1, detail_2 ... from ...
detail_1 detail_2
类型101详细 类型201详细
类型102详细 类型202详细
类型103详细 类型203详细
谢谢!
------解决方案--------------------没看懂你两个表如何匹配,如果只要上面的结果.
select '类型 '+ID_1+ '详细 ' as detail_1 , '类型 '+ID_2+ '详细 ' as detail_2 from B
------解决方案----------------------try this ??
select replace(id_1,id_1,a1.detail) as detail_1,
replace(id_2,id_2,a2.detail) as detail_2
from B
left join A a1 on id_1=a1.id
left join A a2 on id_2=a2.id
------解决方案-------------------- create table A(ID int, TYPE varchar(20), DETAIL varchar(20))
insert A select 101, '类型1 ', '类型101详细 '
union all select 201, '类型2 ', '类型201详细 '
union all select 103, '类型1 ', '类型103详细 '
union all select 104, '类型1 ', '类型104详细 '
go
create table B(ID_1 int, ID_2 int)
insert B select 101, 201
union all select 102, 202
union all select 103, 203
go
select
detail_1=tmp1.DETAIL,
detail_2=tmp2.DETAIL
from B
left join A as tmp1 on B.ID_1=tmp1.ID
left join A as tmp2 on B.ID_2=tmp2.ID
--result
detail_1 detail_2
-------------------- --------------------
类型101详细 类型201详细
NULL NULL
类型103详细 NULL
(3 row(s) affected)