I have a JOIN query that pulls the last 6 "topic" records (the records with the highest topic_ids that are have a topic_status of 0, i.e. it's not spam):
SELECT topic_title, topic_slug, meta_value, topic_poster
FROM `folio_topics` as Topics
INNER JOIN `folio_topicmeta` as Topicmeta
ON Topics.topic_id = Topicmeta.topic_id
WHERE `topic_status` = 0
AND Topicmeta.meta_key = 'bb_attach_thumb'
ORDER BY Topics.`topic_id` DESC
LIMIT 6
I would like to edit this query it so that the folio_topics.topic_poster field is unique, i.e. so I don't get 6 records with the same topic_poster, but rather 6 records all of which have a unique topic_poster value.
Any help would be appreciated! :-)
Riffing off of Vincent's answer, I came up with a query that correctly pulls the highest topic_id for each topic_poster, but it takes 8.7 seconds, which is extremely slow compared to most queries (which take less than 0.1 seconds). Is there a way to optimize this query?
SELECT topic_title, topic_slug, meta_value, topic_poster
FROM `folio_topics` AS Topics
INNER JOIN `folio_topicmeta` AS Topicmeta
ON Topics.topic_id = Topicmeta.topic_id
WHERE Topics.topic_id IN (
SELECT MAX(`topic_id`)
FROM `folio_topics`
WHERE `topic_status` = 0
GROUP BY `topic_poster`)
AND Topicmeta.meta_key = 'bb_attach_thumb'
ORDER BY Topics.`topic_id` DESC
LIMIT 6