CHANGE TO CLOSE
Perhaps I misunderstand the use GROUP BY, so I simply rephrase my question without making assumptions on how to solve the problem:
I have a list term_idand a table containing objects (which have object_idPK and term_idlike FK among other fields), I need to extract the object with the highest object_idfor each term_id. What is the right way to do this?
ORIGINAL QUESTION
I am sure that I am missing something obvious, but I can’t figure out how to indicate which record will be returned by the request with GROUP BY. By default it GROUP BYreturns the first record in the group, who can I get the last instead instead of using a subquery?
The main query returns the first record:
SELECT *
FROM wp_term_relationships
WHERE term_taxonomy_id IN (20, 21, 22)
GROUP BY term_taxonomy_id
it works, but with a subquery
SELECT *
FROM (
SELECT *
FROM wp_term_relationships
WHERE term_taxonomy_id IN (20, 21, 22)
ORDER BY object_id DESC
) wtt
GROUP BY term_taxonomy_id
this is a syntax error
SELECT *
FROM wp_term_relationships
WHERE term_taxonomy_id IN (20, 21, 22)
ORDER BY object_id DESC
GROUP BY term_taxonomy_id
source
share