Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 02-02-2005, 06:06 AM   PM User | #1
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
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?
belteshazzar is offline   Reply With Quote
Old 02-02-2005, 06:48 AM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,435
Thanks: 2
Thanked 38 Times in 38 Posts
Brandoe85 will become famous soon enough
Whichever field is your field that holds the datetime of when the post was made, you can: order by dateField Desc limit 5.
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 02-02-2005, 07:00 AM   PM User | #3
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
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.
belteshazzar is offline   Reply With Quote
Old 02-02-2005, 07:04 AM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,435
Thanks: 2
Thanked 38 Times in 38 Posts
Brandoe85 will become famous soon enough
What are structures of your tables?
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 02-02-2005, 07:09 AM   PM User | #5
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
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?
belteshazzar is offline   Reply With Quote
Old 02-02-2005, 07:15 AM   PM User | #6
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
tables

my tables are (simplified):

posts ( id int,thread int,title varchar,body text,posted datetime);
threads( id int, forum int, title varchar, body text, posts int );
forums ( id int, title varchar );
the tables are joined on:

posts.thread=threads.id
threads.forum=forums.id
belteshazzar is offline   Reply With Quote
Old 02-02-2005, 12:15 PM   PM User | #7
Kiwi
Regular Coder

 
Join Date: Oct 2002
Posts: 379
Thanks: 0
Thanked 0 Times in 0 Posts
Kiwi is an unknown quantity at this point
I'm unsure what the problem is:
PHP Code:
SELECT 
 P
.id,
 
P.title as PostTitle
 
MAX(P.posted)
 
T.id,
 
T.title as TheadTitle,
 
F.id,
 
F.title as ForumTitle
FROM
 posts P
,
 
threads T,
 
forums F
WHERE P
.thread T.id
 
AND T.forum F.id
GROUP BY
 P
.id,
 
PostTitle,
 
T.id,
 
ThreadTitle,
 
F.id,
 
ForumTitle 
__________________
Strategy Conscious

Last edited by Kiwi; 02-02-2005 at 12:18 PM.. Reason: it's tired and i'm late
Kiwi is offline   Reply With Quote
Old 02-03-2005, 03:05 AM   PM User | #8
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
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);
unfortuantely, i'm using a version earlier than 4.1 that doesn't support the above so i have to use temporary tables (according to the mysql docs).

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
belteshazzar is offline   Reply With Quote
Old 02-04-2005, 09:18 PM   PM User | #9
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
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
raf is offline   Reply With Quote
Old 02-06-2005, 02:19 AM   PM User | #10
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
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!
belteshazzar is offline   Reply With Quote
Old 02-07-2005, 04:37 AM   PM User | #11
belteshazzar
New to the CF scene

 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
belteshazzar is an unknown quantity at this point
but ... is it possible to order it by max(posted) somehow ... so in mysql version 4.0 i'm stuck creating a temp table.
belteshazzar is offline   Reply With Quote
Old 02-07-2005, 09:04 PM   PM User | #12
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
Quote:
Originally Posted by belteshazzar
but ... is it possible to order it by max(posted) somehow ... so in mysql version 4.0 i'm stuck creating a temp table.
SELECT
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
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:11 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.