在php上连接两个具有相同列的列
我有这样的桌子在php上连接两个具有相同列的列,php,mysql,mysqli,Php,Mysql,Mysqli,我有这样的桌子 id | title 1 | title john 2 | title alicia 3 | title alex 用户 id | name 1 | john 2 | alicia 3 | alex 遵循表格 id | follow | follower 1 | john | alex 2 | alicia | alex 帖子 id | title | content | name 1 |
id | title
1 | title john
2 | title alicia
3 | title alex
用户
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 | 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
感谢您已经愿意提供帮助您可以使用CONCAT()
或CONCAT_WS()
作为派生字段来连接两列的结果
SELECT
CONCAT_WS(' ', title, name) as full_name
FROM
//...
在这种情况下,您必须使用alias,还必须实现select查询,我已经尝试过了,效果很好
<?php
$select_one = select * from follow_table where id = '$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 posts.name as posted_name,posts.* 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'];
echo $posts['posted_name'];
}
}
替换$select_one
:
SELECT name
FROM user
JOIN follow_table ON (
follow_table.follower = user.name
OR follow_table.follow = user.name
)
WHERE follower = '$id';
这将检索每个用户,后跟alex、和alex本人。这是分配吗?lol:D