Jump to content

selecting using distinct


jwk811

Recommended Posts

im trying to select the posts that a specific user has commented. its returning multiple posts and im using DISTINCT so i dunno if thats the only problem or not. please help

 

	$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
			WHERE tbl_comments.comment_type = 'forum' 
				AND tbl_comments.post_id = forum_posts.post_id 
				AND forum_posts.user_id = '$user_id'

					ORDER BY tbl_comments.date DESC LIMIT 10";
$result = dbQuery($sql);

table structures

 

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)

Link to comment
Share on other sites

distinct will return multiple posts as that's not its purpose.  it will NOT return identical fields, ie. if there are several 'forum_posts.post_name' with the exact same value.

 

i'm not 100% what you're trying to accomplish.

 

and what is dbQuery()?  did you create an alias function for mysql_query()?

Link to comment
Share on other sites

distinct will return multiple posts as that's not its purpose.  it will NOT return identical fields, ie. if there are several 'forum_posts.post_name' with the exact same value.

 

i'm not 100% what you're trying to accomplish.

 

and what is dbQuery()?  did you create an alias function for mysql_query()?

im trying to select the POSTS that a specific user commented. in the list of these posts it is repeating posts that the user posted in multiple times. i only want that post to show once. and yes dbQuery is for mysql query

 

and ideas?

Link to comment
Share on other sites

first things first.  creating aliases are redundant.  use built-in PHP functions instead, ie. mysql_query() instead of dbQuery().

 

try GROUP BY instead of DISTINCT:

 

$sql = "
SELECT `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`
WHERE `tbl_comments`.`comment_type` = 'forum' 
AND `tbl_comments`.`post_id` = `forum_posts`.`post_id`
AND `forum_posts`.`user_id` = '$user_id'
GROUP BY `post_name`
ORDER BY `tbl_comments`.`date`
DESC LIMIT 10
";
$result = dbQuery($sql);

 

consider using the MySQL Help forum next time.  those guys are good.

 

EDIT: already moved.

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.