I have a metadata table for software package updates. The table has columns id, name, version . I want to select all the lines in which the name is one of the defined lists of names and the version is the maximum of all lines with that name.
For example, given these entries:
+----+------+---------+ | id | name | version | +----+------+---------+ | 1 | foo | 1 | | 2 | foo | 2 | | 3 | bar | 4 | | 4 | bar | 5 | +----+------+---------+
And the task "give me the highest versions of the entries" foo "and" bar ", I want the result to be:
+----+------+---------+ | id | name | version | +----+------+---------+ | 2 | foo | 2 | | 4 | bar | 5 | +----+------+---------+
I am currently using nested queries:
SELECT * FROM updates WHERE ( id IN (SELECT id FROM updates WHERE name = 'foo' ORDER BY version DESC LIMIT 1) ) OR ( id IN (SELECT id FROM updates WHERE name = 'bar' ORDER BY version DESC LIMIT 1) );
It works, but it feels wrong. If I want to filter more names, I have to replicate the entire subquery several times. Is there a better way to do this?
source share