Jump to content

comlicated db SELECT FROM DATABASE


jwk811

Recommended Posts

if you can tell what im doing. im trying to select forum posts that have new comments which are newer than the last time i have read that particular topic.

 

	$sql = "SELECT DISTINCT forum_posts.post_name AS post_name, forum_posts.post_id AS post_id, tbl_comments.user_id AS commenter_id
		FROM tbl_comments, forum_posts, topics_read
			WHERE tbl_comments.comment_type = 'forum' 
				AND tbl_comments.post_id = forum_posts.post_id
				AND topics_read.topic_id = forum_posts.post_id
				AND topics_read.user_id = '$member_user_id'
				AND topics_read.read_at > tbl_comments.date";

 

im getting some rows, dunno why im getting these. there are repeated post names and its definently not giving me the results i wanted.

 

what is wrong here?

Link to comment
Share on other sites

forum_posts

-post_id

-post_name

-post_text

-user_id

 

*that table has all the forum topics

 

tbl_comments

-comment_id

-comment_type (forum, photo, video, etc.)

-date (time posted)

-user_id

-post_id (id of the forum post)

 

*these are the comments that people write for each forum post topic

 

topics_read

-user_id

-topic_id

-read_at

 

*everytime someone goes to a forum post. it says the user and the topic and what time they looked at it, when they go back it just changes the read_at time

 

topics_read.read_at AND tbl_comments.date are both DATETIME

Link to comment
Share on other sites

SELECT DISTINCT 
forum_posts.post_name AS post_name, 
forum_posts.post_id AS post_id, 
tbl_comments.user_id AS commenter_id
FROM 
tbl_comments 
	NATURAL JOIN forum_posts
		ON (tbl_comments.post_id = forum_posts.post_id)
	NATURAL JOIN topics_read
		ON (topics_read.topic_id = forum_posts.post_id)  
WHERE
tbl_comments.comment_type = 'forum'
        AND topics_read.user_id = '{$member_user_id}'
        AND tbl_comments.date > topics_read.read_at

 

Try this first. I'll be amazed if it works but it probably won't.

 

Tell me what error gets back if it does fail. ( using mysql_query(...) OR DIE(mysql_error());  )

 

Edit: Changed some syntax.

Link to comment
Share on other sites

seems like a minor error but i wouldnt know how to fix it. i dont understand the natural joining stuff lol

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON (tbl_comments.post_id = forum_posts.post_id) NATURAL JOIN topics_read ' at line 8

 

btw, thanks for helping! :D

Link to comment
Share on other sites

This is rattling my brain =p

 

You see, a small thing is that you're wanting to seelct forum topics with posts > last viewed time.

Thing is, you're also wanting to select a comment poster's id, note how it's single.

 

A problem with that is, if multiple commenters post on the same topic after you have viewed it, SQL will give back an error because it's trying to select a single value from multiple options.

 

I'm still working on it, don't worry :P

Link to comment
Share on other sites

This is rattling my brain =p

 

You see, a small thing is that you're wanting to seelct forum topics with posts > last viewed time.

Thing is, you're also wanting to select a comment poster's id, note how it's single.

 

A problem with that is, if multiple commenters post on the same topic after you have viewed it, SQL will give back an error because it's trying to select a single value from multiple options.

 

I'm still working on it, don't worry :P

well im executing the data using a while loop so it should show all the comments. maybe thats not what you meant..

Link to comment
Share on other sites

SELECT forum_posts.post_id, forum_posts.post_name, COUNT(comment_id)
FROM topics_read LEFT OUTER JOIN tbl_comments 
ON (topics_read.topic_id = tbl_comments.post_id), forum_posts
WHERE topics_read.read_at < tbl_comments.date
AND topics_read.topic_id = forum_posts.post_id
AND topics_read.user_id = '{$member_user_id}' 
GROUP BY (forum_posts.post_id)

 

This query returns each topic's id and name with the number of newly posted comments (since the user last checked).

 

Finally figured it out :D.

 

 

Edit:

 

well im executing the data using a while loop so it should show all the comments. maybe thats not what you meant..

 

Well, you can make a foreach loop for each row returned.

That way, you can get the data for each seperate topic using the topic id (aka post id) as a filter

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.