I would like to ask you that the main table is associated with the child table, and the child table has multiple records, but you only want to get the latest record of the child table (that is, the newly added record).

can you give me a column?

Mar.13,2021

--

-- Table1  Table2  Field1  Field2

-- Table2  AddTime 

SELECT * FROM [Table1] 

WHERE Field1=(
                   SELECT TOP 1 Field2 FROM [Table2] ORDER BY AddTime DESC --AddTime
              )

JOIN twice

 SELECT p.*, c1.*
     FROM parent p
INNER JOIN child AS c1
       ON (p.id = c1.parent_id)
LEFT JOIN child AS c2
       ON (c1.parent = c2.parent_id and c1.createTime < c2.createTime )
    WHERE c2.createTime IS NULL
Menu