日期:2014-05-19  浏览次数:20895 次

晕倒了
请问各位大虾,表A和B,B是A的子表,A与B是一对多的关系,我如何联合查询得到A与B一对一(A里一条数据对应B里最后插入的那条记录,当然B中有添加时间字段),
恳请高手指教。

------解决方案--------------------
假设id为关联字段
select A.* from A,B where A.id=B.id and A.UpdateTime=B.UpdateTime
------解决方案--------------------
select a.*,b.*
from a,b as b1
where a.id= b.id
not exists (select 1 from b where b.时间字段> b1.时间字段)
------解决方案--------------------
select top 1 A.*,B.* FROM A,B where A.AutoId = B.PersonId order by B.迟到时间 desc
------解决方案--------------------
如果你的UpdateTime的值没有重复值,这样就可以了
select A.*,b.* from A,B where A.id=B.id and B.UpdateTime in(select max(B.UpdateTime),B.id from B group by B.id)

------解决方案--------------------
select aa.*,bb.* from A aa left join B bb on bb.PersonId=aa.AutoId and bb.AutoId=(select top 1 AutoId From B where PersonId=bb.PersonId order by UpdateTime desc)