求救一条SQL语句,急,在线等~~~
表:Line:
linecode POL POD
s1 KG1 KG3
s2 KG1 KG2
..
..
表:port:
portcode portshname
KG1 赤湾
KG2 蛇口
KG3 黄浦
..
..
要得到这样的结果,用一条SQL语句该怎么写:
linecode POL POD POLName PODName
s1 KG1 KG3 赤湾 黄浦
s2 KG1 KG2 赤湾 蛇口
POL,POD是关联表port中的portcode的,是属于外键.
------解决方案--------------------select a.*,l.portshname as Polname,d.portshname as Podname
from Line a left join port l on a.pol=l.portcode
left join port d on a.pod=d.portcode
------解决方案--------------------那我寫個子查詢吧
Select
*,
(Select portshname From port Where portcode = A.POL) As POLName,
(Select portshname From port Where portcode = A.POD) As PODName
From
Line A
------解决方案--------------------像沙發寫個聯合查詢即可呀