php - 三个 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 );
解决方案
试试这个 sql。
SELECT * FROM communities c LEFT JOIN communities_follow cf ON c.user_id = cf.user_id where
cf.user_id = '$user_id';
推荐阅读
- node.js - 如何使用 NodeJS 文件正确呈现自定义元素?
- csv - 如何从 Google 表格中的选定范围创建 CSV 文件?
- javascript - 如何从 JSON/AJAX 截断数据表单元格字符串解析
- kubernetes - 在 Openshift 上外部访问 Kafka
- node.js - Sequelize 计算字段 - VIRTUAL
- c# - Task.Delay 应该等待完全延迟吗?
- nginx - nginx-prometheus-exporter 部署失败
- python - 变量没有在不同的函数中定义
- bokeh - 如何在 Bokeh Div 中显示绘图?
- javascript - In the textbox if user enters minus sign in between or anywhere except for first position, it should jump back to front