Jump to content

SELECT DISTINCT and compare multiple tables.


jambla

Recommended Posts

Hello,

 

I want to select distinct values from ‘posts’.

In the ‘posts’ table there are:

 

pst_id - this is the post id for each posted item.

mem_id - this is the member id.

top_id - this is the topic id that the post (pst_id) is under.

posted – this is the time the post (pst_id) was posted.

 

I have no problem with this part:

 

$query = "SELECT DISTINCT pst_id, mem_id, posted FROM posts ORDER BY posts.posted DESC LIMIT 0, 10";

 

It outputs:

 

Latest Forum Posts

Author - Title - Time

100002  100003 8:14 am

100003 100002 5:12 am

100003 100001 5:04 am

100000 100000 8:59 pm

 

Now heres the part I cant seem to do.  I want the Author values (10002 etc…) to look in the ‘members’ table and output the persons name.

 

The members table looks like this:

 

mem_id: 100002

username: Joe

mem_id: 100003

username: Jill

etc…

 

and I want the Title value (10003 etc…) to look in the ‘topics’ table and output the topic subject title.

 

The topics table looks like this:

 

top_id: 100003

mem_id: 100002

subject: Do you like the color blue?

top_id: 100002

mem_id: 100003

subject: Do you like the color red?

etc…

 

I am able to output this from the database without using using DISTINCT.  But I only want one subject to show.

 

I am using MySQL Version: 4.1.10

 

If I forgot any info please let me know as this is my first posting.

 

Thanks for your help!

 

Link to comment
Share on other sites

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.