I have a table 'content' with the following fields:
id (an unique, increasing identifier)
user_id
content1
content2
content3
...
The table can contain data from the same or different user_id. I am trying to select data sorted by one of the content fields. However I only want to select data from distinct 'user_id' and always take the user's latest entry (so the highest id value). I cannot simply group by user_id because grouping happens before sorting.
This is what I am doing at the moment:
SELECT *
FROM `content`
WHERE `content`.`id` = (
SELECT `id`
FROM `content` as `alt`
WHERE `alt`.`user_id` = `content`.`id`
ORDER BY `id` DESC
LIMIT 1 )
ORDER BY content1 DESC
It works but once the table gets bigger, the performance becomes too slow. Can someone give me an advice how to improve this query?