create table 表仓库(仓库id int,仓库名称 nvarchar(10)) insert into 表仓库 select 1, '山东' insert into 表仓库 select 2, '四川' create table 调拨单表(调拨单id int, 发出仓库 int,收货仓库 int) insert into 调拨单表 select 1 , 1 , 2 go select a.调拨单id,b.仓库名称,c.仓库名称 from 调拨单表 a inner join 表仓库 b on a.发出仓库=b.仓库id inner join 表仓库 c on a.收货仓库=c.仓库id go drop table 表仓库,调拨单表 /* 调拨单id 仓库名称 仓库名称 ----------- ---------- ---------- 1 山东 四川(1 行受影响) */
select [调拨单id], (select top 1 [仓库名称] from [表仓库] where t.[发出仓库]=[仓库id]) [发出仓库], (select top 1 [仓库名称] from [表仓库] where t.[收货仓库]=[仓库id]) [收货仓库] from [调拨单表] t;
select id, (select 仓库名称 from 表仓库 where 仓库id=发出仓库) [发出仓库名称], (select 仓库名称 from 表仓库 where 仓库id=收获仓库) [收货仓库名称] from 调拨单表
insert into 表仓库 select 1, '山东'
insert into 表仓库 select 2, '四川'
create table 调拨单表(调拨单id int, 发出仓库 int,收货仓库 int)
insert into 调拨单表 select 1 , 1 , 2
go
select a.调拨单id,b.仓库名称,c.仓库名称
from 调拨单表 a inner join 表仓库 b on a.发出仓库=b.仓库id
inner join 表仓库 c on a.收货仓库=c.仓库id
go
drop table 表仓库,调拨单表
/*
调拨单id 仓库名称 仓库名称
----------- ---------- ----------
1 山东 四川(1 行受影响)
*/
select [调拨单id],
(select top 1 [仓库名称] from [表仓库] where t.[发出仓库]=[仓库id]) [发出仓库],
(select top 1 [仓库名称] from [表仓库] where t.[收货仓库]=[仓库id]) [收货仓库]
from [调拨单表] t;
select id,
(select 仓库名称 from 表仓库 where 仓库id=发出仓库) [发出仓库名称],
(select 仓库名称 from 表仓库 where 仓库id=收获仓库) [收货仓库名称]
from 调拨单表