1

ユーザーが関連付けられているすべての投稿を返す次のクエリがあります (これはうまく機能しています)

例えば

//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を使用しますか?

編集: クエリに投稿が必要なだけで、コメントはそこにある必要はありません。したがって、投稿のコメントにタグ付けされている場合は、投稿をクエリに含めます。

ヘルプ/ガイダンスをいただければ幸いです。

4

1 に答える 1

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  
于 2012-12-18T11:04:54.080 に答える