Last record of Join table

后端 未结 7 1011
忘了有多久
忘了有多久 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条回答
  •  萌比男神i
    2021-01-06 16:36

    Try this..

    SELECT D.*,DC1.Comment 
    FROM Deals AS D
       INNER JOIN DealComments AS DC1
            ON D.DealId = DC1.DealID
        INNER JOIN 
        (
            SELECT 
                DealID,
                MAX(CommentTime) AS CommentTime
            FROM DealComments AS DC2
            GROUP BY DealID
        ) AS DC2
            ON DC2.DealId = DC.DealId
                AND DC2.CommentTime = DC1.CommentTime
    

提交回复
热议问题