当前位置: 代码迷 >> Sql Server >> 问个查询语句,该怎么解决
  详细解决方案

问个查询语句,该怎么解决

热度:52   发布时间:2016-04-27 17:01:15.0
问个查询语句
2个表     例如     base   ,   user  
base的字段       id       ,   name
                            1             a
                            2             b
                            3             c

user的字段       id1,   id2,   id3
                            1         2         3
如何查询user表最后显示     a       b       c       在线等~

------解决方案--------------------
create table base(id int, name varchar(10))
insert base select 1, 'a '
union all select 2, 'b '
union all select 3, 'c '

create table [user](id1 int, id2 int, id3 int)
insert [user] select 1, 2, 3

select
id1=A.name,
id2=B.name,
id3=C.name
from [user]
left join base as A on [user].id1=A.id
left join base as B on [user].id2=B.id
left join base as C on [user].id3=C.id

--result
id1 id2 id3
---------- ---------- ----------
a b c

(1 row(s) affected)
  相关解决方案