mysql - querying unique latest post by topic -
i'm trying create active topics section, i'm running query problem.
i have 3 tables: posts, topics, categories (smallest greatest). every post associated topic, every topic associated category.
now, problem displaying recent topic posted uniquely.
here's quick scenario: user x posts in topic a, , in topic b. user y creates topic c , posts in topic a. before user y posted on a, user z posted on topic a.
on active topics list i'd show following:
- topic - y's msg
- topic c - y's msg
- topic b - x's msg
my current sql merely grabs sequence correctly, get's same topics well:
- topic - y's msg
- topic - z's msg
- topic c - y's msg
- topic b - x's msg
topic - x's msg
select topic_subject, post_date, post_msg, post_by, cat_color topics, posts, categories topic_id = post_topic , topic_cat = cat_id order post_date desc
if add group topic_subject or id, original topic creators, such following:
- topic c - y's msg
- topic b - x's msg
- topic - x's msg
do need subquery this? thanks!
you can add not exists
subquery :
select topic_subject, post_date, post_msg, post_by, cat_color topics, posts p, categories topic_id = post_topic , topic_cat = cat_id , not exists ( select 1 posts post_topic = p.post_topic , post_date > p.post_date )
Comments
Post a Comment