日期:2014-05-18 浏览次数:20477 次
用join select a.fhname,c.shdw ,b.fhname from wuliu as c join fh as a on c.fhdw=a.fh join fh as b on c.shdw=b.fh
------解决方案--------------------
join 是实行一对一的
left/right join 会有多对一的情况
------解决方案--------------------
---------------------------------
-- Author: HEROWANG(让你望见影子的墙)
-- Date : 2012-04-20 08:09:41
-- blog : blog.csdn.net/herowang
---------------------------------
IF OBJECT_ID('[wuliu]') IS NOT NULL
DROP TABLE [wuliu]
go
CREATE TABLE [wuliu] (wuliu INT,fhdw INT,shdw INT)
INSERT INTO [wuliu]
SELECT 1,1,2 UNION ALL
SELECT 2,2,3
select * from [wuliu]
IF OBJECT_ID('[fh]') IS NOT NULL
DROP TABLE [fh]
go
CREATE TABLE [fh] (fh INT,fhname VARCHAR(4))
INSERT INTO [fh]
SELECT 1,'上海' UNION ALL
SELECT 2,'北京' UNION ALL
SELECT 3,'南京'
select * from [fh]
IF OBJECT_ID('[main]') IS NOT NULL
DROP TABLE [main]
go
CREATE TABLE [main] (wuzi VARCHAR(4),wuliu INT)
INSERT INTO [main]
SELECT '黄金',1 UNION ALL
SELECT '白银',2 UNION ALL
SELECT '钢铁',3
select * from [main]
select main.wuzi,fhdw=s.fhname,shdw=t.fhname from main join wuliu on main.wuliu=wuliu.wuliu
join fh s on wuliu.fhdw=s.fh
join fh t on wuliu.shdw=t.fh
------解决方案--------------------
楼主上面贴上来的 我试了可以执行的啊