三个select和两个json

ux6nzvsh  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(232)

我创建了一个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 );
fcwjkofz

fcwjkofz1#

“内部连接”将仅为您提供以下社区:

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')
fnvucqvd

fnvucqvd2#

试试这个sql。

SELECT * FROM communities c LEFT JOIN communities_follow cf ON c.user_id = cf.user_id where
cf.user_id = '$user_id';

相关问题