我的数据库中有两个表
blog (id, title, body and userid)
user (id, username,firstname, lastname and about)
我想搜索 blog
中的内容表中查找某些关键字并获取 title
的结果, body
和userid
。我想使用userid
在第二个表中选择查询。这样我就可以显示 name(username)
博客作者。
基本上我想使用result(userid)
第二个查询中的第一个查询 query(userid to username)
modified after suggestion **help me**
我尝试了建议的答案
我认为 INNER Join 可以解决我的问题,但仍然没有解决
现在我的代码是
include_once "connect_to_mysql.php";
$sql = mysql_query("SELECT blog.*, user.username FROM blog INNER JOIN user ON
user.id = blog.userid WHERE (title LIKE '%$search%' OR body LIKE '%$search%')");
// try with or without WHERE condition ...and also try with blog.title
while($row = mysql_fetch_array($sql)){
echo $row['username'];
echo $row['title'];
echo $row['user.username']; //try also
}
但是我什么也没得到,你能告诉我这些代码有什么问题吗
主键和外键有任何问题
最佳答案
你为什么不尝试内在加入一些东西
select blog.*, user.username, user.firstname,user.lastname,user.about from blog
inner join user on user.id = blog.userid
如果需要的话,你可以添加额外的where条件,这样你就不需要2次查询,只需要查询结果集就能给你一切。
关于php - 在第二个查询中使用第一个查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20856021/