mysql查询具有多个注释的帖子

时间:2012-12-18 10:57:42

标签: 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吗?

编辑:我只需要查询中的帖子,评论不需要在那里。因此,如果在帖子的评论中标记包含查询中的帖子。

非常感谢任何帮助/指导。

1 个答案:

答案 0 :(得分: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