![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
how to select newest record from one table joined with another
Hi, I hope someone can help me with a sql problem I'm having. I'm developing a simple bulletin board system. I have a table of posts and a table of threads which get joined on posts.thread=threads.id.
What i want to do is show a list of active threads with the newest posts in each. In other words i need to select 5 distinct threads using the posts table in the selection criteria. I've played around with distinct, group by, order by and max(posts.posted) and have come up with nothing. I tried using the following but it doesn't return the newest post in each thread. SELECT threads.id,posts.title,posts.posted FROM threads,posts WHERE threads.id=posts.thread GROUP BY threads.id ORDER BY posts.posted DESC; any ideas? |
|
|
|
|
|
PM User | #3 |
|
New to the CF scene Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
that doesn't work
I've tried that, but it doesn't work. it will select the 5 newest posts but ignores if they are from the same thread.
I want to make sure the result only has a post from each thread. |
|
|
|
|
|
PM User | #5 |
|
New to the CF scene Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
what i need
I think what i need is an alteration of the following sql.
select thread,title,max(posted) from posts group by thread; the title value is not the value of the record that max(posted) comes from! is there a way of making sure that it is? |
|
|
|
|
|
PM User | #6 |
|
New to the CF scene Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
tables
my tables are (simplified):
the tables are joined on:
|
|
|
|
|
|
PM User | #7 |
|
Regular Coder ![]() Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
I'm unsure what the problem is:
PHP Code:
__________________
Strategy Conscious Last edited by Kiwi; 02-02-2005 at 12:18 PM.. Reason: it's tired and i'm late |
|
|
|
|
|
PM User | #8 |
|
New to the CF scene Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
that code gives a list of posts in order of date when they were posted with their respective threads and forums. however, i want to restrict that so that there is only one post (the newest post) from each thread in the result set.
I've found a partial solution on the mysql site: Code:
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article);
thanks for you help. i should be able to get a solution for what i want. the url, by the way, of the mysql docs that i found this at is: http://dev.mysql.com/doc/mysql/en/ex...group-row.html |
|
|
|
|
|
PM User | #9 |
|
Master Coder ![]() ![]() Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
the problem with kiwi's code is the
GROUP BY P.id, PostTitle, T.id, ThreadTitle, F.id, ForumTitle --> this wount cause any grouping at all. MySQL doesn't follow the standard implementation of GROUP BY and doesn't require that all fields are part of an agregarionfunction or the "group by"-clause. Kiwi's code is what you would expect for a standard SQL-version, but it wouldn't work because each post will become a seperate 'group'. Your "group by" clause can only contain T.id Try: SELECT MAX(P.posted) as PostDate, P.id, P.title as PostTitle, T.id, T.title as TheadTitle, FROM posts P, threads T, WHERE P.thread = T.id GROUP BY T.id If that doesn't work, then post (a part) of a dumpfile of these 2 tables so we can try it out.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html |
|
|
|
|
|
PM User | #10 |
|
New to the CF scene Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Promising, but it doesn't work, the problem being that the date found by max(posts.posted) isn't from the posts.id row that is returned. It seems that in this case mysql returns the date of the newest post in each thread, but returns a random post id.
HANG ON! i dont really need the post id! so it works! thanks for that! |
|
|
|
|
|
PM User | #12 | |
|
Master Coder ![]() ![]() Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
MAX(P.posted) as PostDate, P.title as PostTitle, T.id, T.title as TheadTitle, FROM posts P, threads T, WHERE P.thread = T.id GROUP BY T.id ORDER BY PostDate DESC
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|