2012-12-18 120 views
1

我有以下查询返回的所有帖子,多数民众赞成在用户关联(这是伟大的工作)MySQL查询与多条评论,帖子

//friends 
wallusers.mem_id IN (".$matches.") 
// themselves  
OR wallusers.mem_id =".$user_id." 
//tagged in 
OR wallposts.tagedpersons LIKE '%".$user_id."%' 


    $qry = "SELECT DISTINCT wallposts.p_id,wallposts.type,wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,wallposts.tagedpersons,wallposts.title AS thetitle,wallposts.url,wallposts.description,wallposts.cur_image,wallposts.uip,wallposts.likes,wallposts.userid,wallposts.posted_by,wallposts.post as postdata,wallusers.*, UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,wallposts.date_created 
    FROM wallposts,wallusers 
    where (wallusers.mem_id IN (".$matches.") OR wallusers.mem_id =".$user_id." OR wallposts.tagedpersons LIKE '%".$user_id."%') and wallusers.mem_id = wallposts.userid 
    order by wallposts.p_id desc 
    limit ".$show_more_post.", 10"; 

我现在想添加评论到这个查询中,这样如果有人在评论中标记用户,它将在这个查询中返回。

这将加入的:

wallposts.p_id = wallcomments.post_id 

where子句是:

wallcomments.tagedpersons LIKE '%".$user_id."%' 

有每个岗位多意见,我很努力工作了我将如何实现这一点,我会用INNER JOIN吗?

编辑:我只需要在查询中发表评论不需要在那里。所以如果在帖子的评论中标记了包含在查询中的帖子。

任何帮助/指导将不胜感激。

回答

0

这可能做的工作,是的,你可以使用内部连接

SELECT DISTINCT wallposts.p_id,wallposts.type,wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,wallposts.tagedpersons,wallposts.title AS thetitle,wallposts.url,wallposts.description,wallposts.cur_image,wallposts.uip,wallposts.likes,wallposts.userid,wallposts.posted_by,wallposts.post as postdata,wallusers.*, UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,wallposts.date_created 
FROM wallposts,wallusers 
INNER JOIN wallcomments 
ON wallposts.p_id = wallcomments.post_id 
WHERE (wallusers.mem_id IN (".$matches.") OR wallusers.mem_id =".$user_id." OR wallposts.tagedpersons LIKE '%".$user_id."%') and wallusers.mem_id = wallposts.userid AND wallcomments.tagedpersons LIKE '%".$user_id."%' 
ORDER BY wallposts.p_id desc 
LIMIT ".$show_more_post.", 10 

编辑:

SELECT wallposts.p_id,wallposts.type,wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,wallposts.tagedpersons,wallposts.title AS thetitle,wallposts.url,wallposts.description,wallposts.cur_image,wallposts.uip,wallposts.likes,wallposts.userid,wallposts.posted_by,wallposts.post as postdata,wallusers.*, UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,wallposts.date_created 
FROM wallposts 
INNER JOIN wallusers 
ON wallposts.userid = wallusers.mem_id  // LOOK IF wallusers.mem_id is same as wallposts.userid , im not sure about your table 
INNER JOIN wallcomments 
ON wallposts.p_id = wallcomments.post_id 
WHERE (wallusers.mem_id IN (".$matches.") OR wallusers.mem_id =".$user_id." OR wallposts.tagedpersons LIKE '%".$user_id."%') and wallusers.mem_id = wallposts.userid AND wallcomments.tagedpersons LIKE '%".$user_id."%' 
ORDER BY wallposts.p_id,wallposts.type,wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,wallposts.tagedpersons,wallposts.title 
LIMIT ".$show_more_post.", 10 
+0

这是行不通的预期。不要以为我的问题结构正确。我只想获取该评论的帖子,所以实际上并不需要查询中的评论数据。查询只需要在查询 – Codded

+0

包含相关的帖子得到这个错误#1054 - 'on子句'中的未知列'wallposts.p_id' – Codded

+0

看看我的编辑,如果它为你工作 –