首页
学习
活动
专区
圈层
工具
发布

三选二
EN

Stack Overflow用户
提问于 2018-06-24 06:04:07
回答 2查看 38关注 0票数 0

我创造了一个选择来让我的社区。

创建两个选择来获得我所关注的社区。

但我只有我的社区。

我没有得到我所关注的社区。

代码语言:javascript
复制
$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 );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-24 06:53:10

内在的加入只会让你得到那些社区,在那里你会跟随:

代码语言:javascript
复制
SELECT c.* FROM communities c 
INNER JOIN communities_follow cf ON c.id = cf.coid
WHERE cf.user_id = '$user_id';

或者,没有JOIN

代码语言:javascript
复制
SELECT * FROM communities
WHERE EXISTS (SELECT 1 FROM communities_follow cf
  WHERE c.id = cf.coid AND cf.user_id = '$user_id')
票数 0
EN

Stack Overflow用户

发布于 2018-06-24 06:25:38

试试这个sql。

代码语言:javascript
复制
SELECT * FROM communities c LEFT JOIN communities_follow cf ON c.user_id = cf.user_id where
cf.user_id = '$user_id';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51007363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档