Unique Identifiers? Yes! Unique identifiers!
One of the best ways to develop a MySQL DB is to have each id
AUTOINCREMENT
(Source MySQL.com). This allows a variety of advantages, too many to cover here. The problem with the question is that its example has duplicate ids. This disregards these tremendous advantages of unique identifiers, and at the same time, is confusing to those familiar with this already.
Cleanest Solution
DB Fiddle
Newer versions of MySQL come with ONLY_FULL_GROUP_BY
enabled by default, and many of the solutions here will fail in testing with this condition.
Even so, we can simply select DISTINCT
someuniquefield, MAX(
whateverotherfieldtoselect )
, (
*somethirdfield )
, etc., and have no worries understanding the result or how the query works :
SELECT DISTINCT t1.id, MAX(t1.rev), MAX(t2.content)
FROM Table1 AS t1
JOIN Table1 AS t2 ON t2.id = t1.id AND t2.rev = (
SELECT MAX(rev) FROM Table1 t3 WHERE t3.id = t1.id
)
GROUP BY t1.id;
SELECT DISTINCT Table1.id, max(Table1.rev), max(Table2.content)
: Return DISTINCT
somefield, MAX()
some otherfield, the last MAX()
is redundant, because I know it's just one row, but it's required by the query.
FROM Employee
: Table searched on.
JOIN Table1 AS Table2 ON Table2.rev = Table1.rev
: Join the second table on the first, because, we need to get the max(table1.rev)'s comment.
GROUP BY Table1.id
: Force the top-sorted, Salary row of each employee to be the returned result.
Note that since "content" was "..." in OP's question, there's no way to test that this works. So, I changed that to "..a", "..b", so, we can actually now see that the results are correct:
id max(Table1.rev) max(Table2.content)
1 3 ..d
2 1 ..b
Why is it clean? DISTINCT()
, MAX()
, etc., all make wonderful use of MySQL indices. This will be faster. Or, it will be much faster, if you have indexing, and you compare it to a query that looks at all rows.
Original Solution
With ONLY_FULL_GROUP_BY
disabled, we can use still use GROUP BY
, but then we are only using it on the Salary, and not the id:
SELECT *
FROM
(SELECT *
FROM Employee
ORDER BY Salary DESC)
AS employeesub
GROUP BY employeesub.Salary;
SELECT *
: Return all fields.
FROM Employee
: Table searched on.
(SELECT *...)
subquery : Return all people, sorted by Salary.
GROUP BY employeesub.Salary
: Force the top-sorted, Salary row of each employee to be the returned result.
Unique-Row Solution
Note the Definition of a Relational Database: "Each row in a table has its own unique key." This would mean that, in the question's example, id would have to be unique, and in that case, we can just do :
SELECT *
FROM Employee
WHERE Employee.id = 12345
ORDER BY Employee.Salary DESC
LIMIT 1
Hopefully this is a solution that solves the problem and helps everyone better understand what's happening in the DB.
content
field for the row? – PleopodSELECT DISTINCT ON .... ORDER BY "UserId", "Deals".position;
worked better – MasticateAUTO_INCREMENT
with two fields -- dba.stackexchange.com/q/35449 . That makes this very much so non-standard; if I can't increment its position, how useful is a unique position? – Subinfeudate