sql语句如何将三个表进行关联查询?
有三个表A、B、C进行关联查询,我很急,请各位高手抽那么一小点儿时间帮我解决这个问题,感激不尽!!表A:CaseNoDateRecivedBuildIdcontractN...
有三个表A、B、C进行关联查询,我很急,请各位高手抽那么一小点儿时间帮我解决这个问题,感激不尽!!
表A:
CaseNo DateRecived BuildId contractNo
S001 2010-10-10 SA2008 123467
S002 2010-09-09 SA2005 456445
S008 2010-07-07 SA3004 785454
表B:(有重复时取最大的时间)
CaseNo DateOfRMI
S001 2011-05-05
S001 2011-02-02
S002 2011-04-05
表C:
CaseNo venue District
S001 venue7 hunan
S002 Venue5 changsha
查询后要得到的效果就是:
CaseNo DateRecived BuildId contractNo DateOfRMI venue District
S001 2010-10-10 SA2008 123467 2011-05-05 venue7 hunan
S002 2010-09-09 SA2005 456445 2011-04-05 Venue5 changsha
S008 2010-07-07 SA3004 785454 展开
表A:
CaseNo DateRecived BuildId contractNo
S001 2010-10-10 SA2008 123467
S002 2010-09-09 SA2005 456445
S008 2010-07-07 SA3004 785454
表B:(有重复时取最大的时间)
CaseNo DateOfRMI
S001 2011-05-05
S001 2011-02-02
S002 2011-04-05
表C:
CaseNo venue District
S001 venue7 hunan
S002 Venue5 changsha
查询后要得到的效果就是:
CaseNo DateRecived BuildId contractNo DateOfRMI venue District
S001 2010-10-10 SA2008 123467 2011-05-05 venue7 hunan
S002 2010-09-09 SA2005 456445 2011-04-05 Venue5 changsha
S008 2010-07-07 SA3004 785454 展开
4个回答
展开全部
select DISTINCT (a.CaseNo) CaseNo,
a.DateRecived,
a.BuildId,
a.contractNo,
MAX(b.DateOfRMI) DateOfRMI,
c.venue,
c.District
from a
left join b on a.CaseNo = b.CaseNo
left join c on a.CaseNo = c.CaseNo
你试试,我懒得建表了,不一定对,要是丢人了就别告诉我了哈~~
a.DateRecived,
a.BuildId,
a.contractNo,
MAX(b.DateOfRMI) DateOfRMI,
c.venue,
c.District
from a
left join b on a.CaseNo = b.CaseNo
left join c on a.CaseNo = c.CaseNo
你试试,我懒得建表了,不一定对,要是丢人了就别告诉我了哈~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试这个:有疑问可HI我。
select a.*,B.DateOfRMI,C.venue,C.District
from (select CaseNo,max(DateRecived),BuildId,contractNo from A group by CaseNo)a,B,C
where B.CaseNo=a.CaseNo and C.CaseNo=a.CaseNo
select a.*,B.DateOfRMI,C.venue,C.District
from (select CaseNo,max(DateRecived),BuildId,contractNo from A group by CaseNo)a,B,C
where B.CaseNo=a.CaseNo and C.CaseNo=a.CaseNo
更多追问追答
追问
你的这条语句还是不行哦,查出来的结果是以表B的CaseNo为主,我要的是以表A的CaseNo为主,表A里面有几个caseNo,就必须都显示出来,表A与表B是一对多的关系,当表B有重复的caseNo时取最大的DateOfRMI,并且只显示一个CaseNo
追答
哦,一开始没有完全看清楚
这个应该可以了
select A.*,b.DateOfRMI, C.venue,C.District
from A
left join (select CaseNo,max(DateOfRMI) as DateOfRMI from B group by CaseNo) b
on b.CaseNo=A.CaseNo
left join C on C.CaseNo=A.CaseNo
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
丢……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询