我创建了一个SELECT来获取我的社区。

并创建两个SELECT以获得我关注的社区。

但是我只有我的社区。

我没有关注的社区。

$user_id = $_GET["id"];

$row1 = array();
$row2 = array();

// get my communities

$res1 = mysql_query("SELECT * FROM communities where user_id = '$user_id'");

while($r1 = mysql_fetch_assoc($res1)) {
$row1[] = $r1;
   }

// get "id" of my communities I'm following

$res = mysql_query("SELECT * FROM communities_follow where user_id = '$user_id'");

while($r = mysql_fetch_assoc($res)) {
    $coid = $r["coid"];

// get my communities I'm following

$res2 = mysql_query("SELECT * FROM communities where id = '$coid'");

while($r2 = mysql_fetch_assoc($res2)) {
$row2[] = $r2;
   }


   }

 $resp = array_replace_recursive($row1, $row2);

 print json_encode( $resp );

最佳答案

内部联接只会使您关注以下社区:

SELECT c.* FROM communities c
INNER JOIN communities_follow cf ON c.id = cf.coid
WHERE cf.user_id = '$user_id';


或者,没有JOIN

SELECT * FROM communities
WHERE EXISTS (SELECT 1 FROM communities_follow cf
  WHERE c.id = cf.coid AND cf.user_id = '$user_id')

10-06 05:42