请问这个简单的SQL语句怎么写?

ocool 2003-12-15 04:51:21
第一个表Port结构:
ID(int4) Name
内容 1 SHANGHAI
2 TIANJIN
3 QINGDAO

第二个表Lane结构:
ID(int4) LaneID PortID1 PortID2 PortID3
内容 1 1 1 2 3

现在根据Lane的LaneID值(例如1),返回Port1,Port2,Port3的Name,就是SHANGHAI,TIANJIN,QINGDAO,
请问这个简单的SQL语句怎么写?
...全文
69 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ocool 2003-12-16
  • 打赏
  • 举报
回复
up
azsoft 2003-12-16
  • 打赏
  • 举报
回复
zjcxc(邹建):

select id,lanid
,portname1=(select name from Port where id=a.PortID1)
,portname2=(select name from Port where id=a.PortID2)
,portname3=(select name from Port where id=a.PortID3)
from Lane a
vileboy 2003-12-15
  • 打赏
  • 举报
回复
再改:
select p1.name as port1 ,p2.name as port2,p3.name as port3 from
port as p1 right join
port as p2 right join
port as p3 right join
lane as la
on p3.id=la.protid3
on p2.id=la.protid2
on p1.id=la.protid1
vileboy 2003-12-15
  • 打赏
  • 举报
回复

select p1.name,p2.name,p3.name from
port as p1 right join
port as p2 right join
port as p3 right join
lane as la
on p3.id=la.protid3
on p2.id=la.protid2
on p1.id=la.protid1
zjcxc 元老 2003-12-15
  • 打赏
  • 举报
回复

select id,lanid
,portname1=(select name from Port where id=a.PortID1)
,portname2=(select name from Port where id=a.PortID2)
,portname3=(select name from Port where id=a.PortID3)
from Lane a
yyaoi 2003-12-15
  • 打赏
  • 举报
回复
select a.laneid,a.portid1,b.name from lane a,port b
where a.portid1=b.id
union select a.laneid,a.portid2,b.name from lane a,port b
where a.portid2=b.id
union select a.laneid,a.portid3,b.name from lane a,port b
where a.portid3=b.id

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧