我有这样的 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/