Last record of Join table

后端 未结 7 969
忘了有多久
忘了有多久 2021-01-06 16:05

I am lookign for the correct SQL code to join 2 tables and show only the last record of the details table.

I have a DB with 2 tables,

Deals 
   Deal         


        
相关标签:
7条回答
  • 2021-01-06 16:55

    Obligatory no-subquery-nowhere-answer:

    select d.*
           , dc.*
    from   Deals d
           left outer join DealComments dc 
           on d.DealID = dc.DealID
           left outer join DealComments dc1 
           on d.DealID = dc1.DealID 
       and 
           dc1.CommentTime > dc.CommentTime
    where  dc1.CommentTime is null
    

    Show me everything in Deals and DealComments when there exists no CommentTime greater than any given comment time for a particular DealID.

    Edit: as Alex Kuznetsov astutely points out in a comment: the OP requested that all deals be displayed -- whether a deal has a comment or not. So I have changed the first JOIN from INNER to LEFT OUTER.

    0 讨论(0)
提交回复
热议问题