Sql Server语句
假设有两张表A 和B
A表
GameID GameName
1 aaa
2 bbb
3 ccc
.. ...
B表
RoomID GameID RoomName
1 2 ddd
2 2 ggg
3 3 fff
4 1 hhh
5 3 ttt
.. .. ...
用slq 语句 查询B表中所有的信息,把GameID转换成A表的GameName 我记得是用存储过程吗???
[解决办法]
一条sql语句就可以了,不需要用存储过程
;with ta(GameID,GameName) as
(
select 1,'aaa'
union all select 2,'bbb'
union all select 3,'ccc'
),
tb(RoomID, GameID,RoomName) as
(
select 1,2,'ddd'
union all select 2,2,'ggg'
union all select 3,3,'fff'
union all select 4,1,'hhh'
union all select 5,3,'ttt'
)
select RoomID,a.GameName,b.RoomName
from tb b
left join ta a on b.GameID=a.GameID
/*
RoomIDGameNameRoomName
-------------------------------
1bbbddd
2bbbggg
3cccfff
4aaahhh
5cccttt
*/
create table #ta(GameID int,GameName varchar(50))
insert into #ta
select 1,'aaa'
union all select 2,'bbb'
union all select 3,'ccc'
create table #tb(RoomID int, GameID int,RoomName varchar(50))
insert into #tb
select 1,2,'ddd'
union all select 2,2,'ggg'
union all select 3,3,'fff'
union all select 4,1,'hhh'
union all select 5,3,'ttt'
select RoomID,a.GameName,b.RoomName
from #tb b
left join #ta a on b.GameID=a.GameID
--------------------------RoomID GameName RoomName
----------- -------------------------------------------------- --------------------------------------------------
1 bbb ddd
2 bbb ggg
3 ccc fff
4 aaa hhh
5 ccc ttt
(5 行受影响)