[英]Select data from 2 tables mysql
我是堆栈溢出的新手。 我有一个问题,如果有人知道,请帮助我.... 我在我的 Web 应用程序中构建了一个关注者系统,用户可以在其中关注其他人,然后他可以在他的时间轴上看到更新。 我有2张桌子
| tbl_follow |
|---------------------------
| id | follow | follow_by |
| 01 | joe | mark |
----------------------------
第二张表是:
| timeline_tbl |
|-------------------------------
| id | staus | username |
| 01 | demo text | joe |
| 01 | demo text | adem |
--------------------------------
现在假设登录用户是“mark”。 所以马克将只能在他的时间线上看到“乔”状态,因为他跟着他(见 tbl_follow)“有人可以帮助我......并提前感谢您的时间并为我的英语不好而抱歉
目前我使用了这个代码
$sql = ("select * from tbl_follow where username = 'mark' ");
while($rows=mysql_fetch_assoc($sql))
{
$follow=$rows["follow"];
$asql = ("select * from timeline where username = '$follow' ");
$get = mysql_fetch_assoc($asql);
$status = $get['status'];
echo $status "<br>";
}
尝试这个:
$sql = "select timeline_tbl.* from tbl_follow JOIN timeline_tbl ON timeline_tbl.username = tbl_follow.follow where follow_by = 'mark' "
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["staus"];
}
select t.*
from timeline_tbl t
join tbl_follow f on f.username = t.follow
where t.follow = 'mark'
它将返回所有跟随 MARK 的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.