tags:

views:

94

answers:

2

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
+1  A: 

According to this article you may use a Group By to accomplish what you are trying. Try this

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'
Group By topic_poster
ORDER BY Topics.`topic_id` DESC
LIMIT 6
Vincent Ramdhanie
Awesome, this works! Thank you Vincent!
bobbyh
Actually, this seems to pull the *lowest* topic_id for each topic_poster and I'm trying to get the *highest* topic_id. Is there a way to tweak the query so it shows the highest topic_id available for each topic_poster?
bobbyh
Just something to try - change the order by to asc instead of desc in the original query. so you end up with: ORDER BY Topics.`topic_id` ASC -- does that do what you want?
Vincent Ramdhanie
Replacing DESC with ASC in the query I put in the Question returns the first 6 topics, with non-unique topic_posters. Replacing DESC with ASC in the query you have in this answer returns the first 6 topics with unique topic_posters. The desired result-set is the last 6 topics with unique topic_posters.
bobbyh
A: 

In case anybody comes across this, I could not find a way to make that huge query run quickly, so I split it into two queries in code. Here's the PHP code:

$folio_posters = $wpdb->get_results("SELECT MAX( `topic_id` ) as topiclist FROM `folio_topics` WHERE `topic_status` =0 GROUP BY `topic_poster` ORDER BY topiclist DESC LIMIT 6");

foreach ($folio_posters as $folio_poster) {
    $folio_poster_list[] = $folio_poster->topiclist;
}

$foliopics = $wpdb->get_results("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 (" .  implode ($folio_poster_list, ',') . ") AND Topicmeta.meta_key = 'bb_attach_thumb' ORDER BY Topics.`topic_id` DESC");

If somebody figures out how to do it with one query, I'll accept that version. :-)

bobbyh