V
V
Vasya Borisevich2020-03-03 13:24:28
MySQL
Vasya Borisevich, 2020-03-03 13:24:28

How to choose the last unique value?

There is a table with data
5e5e2d3aab3c7682598566.png

From it you need to pull out the last records with a unique value of the column "counter_id" I'm trying

to make such a query

SELECT * FROM (SELECT * FROM countersRecords ORDER BY id DESC) AS gagaga GROUP BY gagaga.counter_id


As a result, 2 and 3 identifiers should be selected, and 1 and 3 are selected.

Code
SELECT * FROM countersRecords GROUP BY counter_id ORDER BY id ORDER BY id DESC

also gives no results.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Ushenin, 2020-03-03
@ki11k4

SELECT r.id
FROM countersRecords r
WHERE r.id = (
    SELECT MAX(id)
    FROM countersRecords latest
    WHERE latest.counter_id = r.counter_id
) t

But it will not work very fast, especially if the table is large.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question