When querying data, if a column is duplicated, take the latest record of the column

For example, in the figure below, sn is s002 and there are three duplicate data. When requesting a query, s002 only takes the one with Id=4

The method is to use group by, use Id as a unique identifier (self-increasing), arrange by sn, and then take the largest Id of duplicate items (non-repeated items directly take the unique Id), thereby filtering out duplicate data.

sql code is

select * from test where Id in (select max(Id) from test group by sn)

The result obtained is:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325308380&siteId=291194637