Jump to content

Query Help Please! Trying to limit select records from 1 table based on another.


turboterror

Recommended Posts

Hi all!

Long time reader of the forums, first time poster!

 

First table design:

Topics table:

 

id (int 10)

topic_id (int 10)

topic_title (tinytext)

 

Replies table:

topic_id (int 10)

reply_id (int 10)

posters_name (varchar 255)

 

I have a problem, I am trying to select all topics from the Topics table where there has been no reply. When a reply is made, the topic_id is inserted into the Replies table. So if I replied to topic id #10, the replies table would now include a record with the topic_id #10, the id to the reply (not important) and the name of the poster who replied (posters_name). Pretty simple design.

 

Let's assume I am doing this query for the poster named "timmy". My question is this, how do I select all rows from Topics where timmy hasn't replied to that topic? I've tried queries such as:

 

$query = mysql_query("SELECT DISTINCT * FROM Topics JOIN Replies ON Topics.topic_id != Replies.topic_id AND Replies.posters_name = 'timmy' LIMIT 0,1");

 

This query would work great if I removed the AND condition (Replies.posters_name = 'timmy') and wasn't picky about who made the reply. Since I want to select only those rows where timmy didn't reply, I figured the AND condition would work, but it falls on it's face miserably.

 

Please help me out, I would really appreciate it!

 

James

Link to comment
Share on other sites

Hi Illusion,

Thanks for the suggestion but that will not work because I am looking for topic_id's within Topic which are NOT in Replies. Your query was unable to find any matches as there were no Replies, instead it should found 100% of the Topic's table as there is no Replies in the test db right now.

 

Any other suggestions?

 

Thanks!

Link to comment
Share on other sites

"When a reply is made, the topic_id is inserted into the Replies table" ----

and u r also checking this condition Replies.posters_name = 'timmy'

You mentioned in the previous post that the test DB for replies table is empty so the condition to check for the posters name would fail and the query will be empty.

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.