SQL - Select unique rows from a group of results

前端 未结 5 1121
时光说笑
时光说笑 2021-01-25 14:44

I have wrecked my brain on this problem for quite some time. I\'ve also reviewed other questions but was unsuccessful.

The problem I have is, I have a list of results/t

5条回答
  •  面向向阳花
    2021-01-25 15:27

    This should work in MySQL:

    SELECT registration, id, date, unittype FROM 
      (SELECT registration AS temp_reg, MAX(date) as temp_date 
      FROM table_name GROUP BY registration) AS temp_table
    WHERE registration=temp_reg and date=temp_date
    

    The idea is to use a subquery in a FROM clause which throws up a single row containing the correct date and registration (the fields subjected to a group); then use the correct date and registration in a WHERE clause to fetch the other fields of the same row.

提交回复
热议问题