Jump to content

[SOLVED] Question on getting/using info from 2 tables


Recommended Posts

I have a table "topics" with the columns:

 

ID, ID_board, name, description, views, order, locked

 

and then a table "posts" with the columns:

 

ID, ID_topic, ID_board, ID_poster, posttime, modifier, subject, body, modifiedtime

 

===================

 

What I want to do is create a query that

 

SELECTS

 

topics.ID, topics.name, topics.description, topics.views, topics.order, topics.locked

 

AND

 

posts.posttime (but ONLY on the post that has "posts.ID_topic = $id_topic" with the latest posts.posttime)

 

WHERE

 

topics.ID_board = $id_board, posts.ID_board

 

ORDER BY

 

posts.posttime DESC

 

=====================

 

Hope that is clear enough...

You sort of almost answered your own issue.

 

SELECT t.ID, t.name, t.description, t.views, t.order, t.locked, p.posttime
FROM topics t
INNER JOIN posts p
     ON posts.ID_topic = $id_topic
WHERE t.ID_board = $id_board
     AND t.ID_board = p.ID_board
ORDER BY p.posttime DESC;

 

Is that right?

I'm not sure, because "$id_board" isn't really available... except within the query itself...

 

It's going to loop, and I want it to get all the topic information as well as information on the last post within that topic.

 

while($topics = mysql_fetch_array($result))

{

  [printing information like topic title/description as well as information on the time of the last post which i want it sorted by]

}

Hi

 

Think something like this would do what you want.

 

SELECT topics.ID, topics.name, topics.description, topics.views, topics.order, topics.locked, postSub.LatestPostTime
FROM topics
LEFT OUTER JOIN (SELECT ID_board, MAX(posttime) AS LatestPostTime FROM posts GROUP BY ID_board) postSub
ON topics.ID_board = postSub.ID_board
ORDER BY postSub.LatestPostTime DESC

 

Basically join the topics table with a subselect of the posts table.

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.