首页 > 解决方案 > 三个 SELECT 和两个 json

问题描述

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

标签: phpmysqljson

解决方案


试试这个 sql。

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

推荐阅读