Jump to content

Need to INNER JOIN two tables but forgot how...


wimpster

Recommended Posts

Hey,

 

I have two tables, posts and threads.

 

I'm using a $_GET to determine which thread the user wants to view, so for example we want to see thread #3.

 

$getPosts = mysql_query("SELECT * FROM threads,posts WHERE threads.id=3 AND posts.thread_id=3 ORDER BY postdate ASC");

 

That is basically how it looks like right now, I've done inner joins before but completely forgot how to. But to sum up what I want done;

 

I want this thread to show the threadpost from the table 'threads' first, and then the posts from 'posts' for this thread following.

 

The posts have a column named 'thread_id' that if corresponding with the thread, will list the posts within the thread, meaning 'thread_id=3'

 

In both tables I have such things like author, postdate, message, ip, and etc etc, but only 'threads' table contains the column 'topic'

 

I know it looks pretty messy, I could just screw this idea and have both threads and posts in the same table, defining a post as a thread in a column and etc etc but I feel like trying this method.

 

Just ask if you got any questions, I'm sure I missed some information that needed to be told but it's messy as I said before.

 

Thanks!

SELECT t.`topic`,t.`author` AS `thread_author`, t.`postdate` AS `thread_postdate`, t.`message` AS `thread_message`, p.`author` AS `post_author`, p.`postdate` AS `post_postdate`, p.`message` AS `post_message` FROM `threads` as `t` LEFT JOIN `posts` AS `p` ON t.`id`=p.`thread_id` WHERE t.`id`=3

 

this will get a joined result of threads/posts with a thread ID of 3.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.