2013-10-07 48 views
0

我很难将select语句的结果与另一个表的数据库结合起来。如何将select语句的结果与其他数据库的其他表结合使用

这是我的select语句之一,它工作得很好:

数据库名称 “LIS” 表 “Legal_Records

Select CaseNo,Judicial_level,Received_date,Due_Date, (SELECT Max(cast(Due_date as datetime)) FROM Legal_Records subc WHERE subc.CaseNo=c.CaseNo Group by c.CaseNo) AS MaxDue_Date from Legal_Records c 

结果:

enter image description here

这是另一个查询,它也是工作KS罚款:

数据库名称: “Pandimandata2002” 表名: “tblCrew

Select CaseNo, Lastname,Vessel,Status from tblCrew where Pandimandata2002.dbo.tblCrew.CaseNo like '%CRW%' and (Status not like '%clos%' and Status not like '%settl%' and Status not like '%ca%cel%') and Status like '%court%' and ClubCode like '%TR%' 

结果:

enter image description here

我想要做的就是添加列当CaseNo匹配到另一个表时,从数据库“LIS”表“Legal_Records”到数据库“Pandimandata2002”的表“tblCrew”的名称“Judicial_level”。

我希望我让自己清楚!谢谢你..

回答

0
Select lr.Judicial_level, tc.* 
FROM LIS.dbo.Legal_Records lr 
INNER JOIN Pandimandata2002.dbo.tblCrew tc 
ON lr.CaseNo = tc.CaseNo 
+0

谢谢你的答案,但你可以使它更具体基于我的示例查询,因为它是在不同的数据库。 TIA! –

+0

您是否尝试过先运行查询?查询本身应该检索所有记录。从那里,你可以继续添加过滤器。 – iceheaven31

+0

正如你所看到的,我对第一个查询有两个select语句,以便根据最新的到期日获取最新的司法级别。所以它不会得到我想要的结果。 –