php - 在 php 上加入具有相同列的两列

标签 php mysql mysqli

我有这样的 table

用户

id |  name 
1  |  john 
2  |  alicia 
3  |  alex

关注表

id |  follow  | follower
1  |  john    | alex
2  |  alicia  | alex

帖子

id |  title         | content | name
1  |  title john    | ....... | john  
2  |  title alicia  | ....... | alicia 
3  |  title alex    | ....... | alex 

例子 ;

$id = 'alex';

$select_one = select * from follow_table where follower = '$id';
    $query_one = mysqli_query($mysqli, $select_one) or die (mysqli_error($mysqli));
                 while ($follow = mysqli_fetch_array($query_one)) { 
                 $follow = $follow['follow']; 

$select_two = select * from posts where name = '$follow';
    $query_two = mysqli_query($mysqli, $select_two) or die (mysqli_error($mysqli));
                 while ($posts = mysqli_fetch_array($query_two)) {  

                 echo $posts['title'];

                  }
                 }

结果会像这样:

id  | title
1   | title john    
2   | title alicia  

上面的实际sql是正确的,但我只是想在$follow中添加一个查询。

我的问题是,如何让 user.id 3 (alex) 进入字段 $follow? 我想要这样的东西

id  | title
1   | title john    
2   | title alicia  
3   | title alex

谢谢你已经愿意提供帮助

最佳答案

$select_one 替换为:

SELECT name
FROM user
JOIN follow_table ON (
       follow_table.follower = user.name 
    OR follow_table.follow = user.name
)
WHERE follower = '$id';

这将检索亚历克斯、亚历克斯本人之后的每个用户。

关于php - 在 php 上加入具有相同列的两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21851516/

相关文章:

php - 将 PHP mySQL 查询输出到下拉列表中

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

内存不足错误后PHP自动添加错误

带有 OpenSSL 签名证书的 PHP Websocket SSL Stunnel "bad certificate"

php - Laravel 5 Session_driver 数据库 token 不匹配

mysql - 如何使用mysql随机排序结果?

mysql - Node MySQL 连接池 - 等待数据库启动

php - 使用 PHP 脚本从下拉列表更新 SQL 数据库

php - 服务器端 PHP 代码将表单处理到 mysql

php - bash.sh 运行 cron 的权限被拒绝