mysql - 使用 Sql 子查询显示帖子

标签 mysql

我正在为我的部门建立一个小型社交网络。

我的主页是您可以发表评论的地方,您的 friend 也可以对其发表评论。

我有4张 table ...allposts_tb、friendship_tb、response_tb和signup_tb..

allposts_tb 收集由以下人员发布的更新和帖子:all_id(PK),name,评论,时间)

友谊_tb​​ 作为列 (friend_id(Pk),myname,newfriend,状态)

response_tb 为 columns(id(Pk),name,response,all_id( Fk),时间)

signup_tb 作为列(signup_idlastnamefirstnameemail ,密码,国家/地区,个人资料图片)

我可以使用下面的 sql 查询轻松显示人们发表的评论

 SELECT allposts_tb.all_id,allposts_tb.name,allposts_tb.comment, allposts_tb.time,
friendship_tb.myname,friendship_tb.newfriend,signup_tb.firstname,
signup_tb.lastname,signup_tb.email,signup_tb.profilepicture,
allposts_tb.expand   
FROM allposts_tb,friendship_tb,signup_tb
WHERE allposts_tb.name = friendship_tb.newfriend
 and friendship_tb.myname=colname and
 allposts_tb.name=signup_tb.email
ORDER BY allposts_tb.all_id DESC
and colname=$_SESSION['MM_Username']

我在显示人们对评论正下方的特定评论做出的回复时遇到问题...就像复制 Facebook 的页面一样。您立即说些什么,它应该显示在评论正下方... 我知道我应该使用子查询。就在我给你们的查询中……已经阅读了很多网页,但无法让它工作……请帮助我……

最佳答案

您可以创建与第一个查询类似的第二个查询,只不过使用response_tb 而不是allposts_tb 表。然后在两个查询之间放置 UNION ALL 语句,合并结果。最后,您可以添加 ORDER BY 子句并按 all_id 和时间排序。

只需将response_tb 中的查询中的“评论”字段替换为“响应”即可。

SQL 很可能如下所示:

SELECT allposts_tb.all_id,allposts_tb.name,allposts_tb.comment, allposts_tb.time,
friendship_tb.myname,friendship_tb.newfriend,signup_tb.firstname,
signup_tb.lastname,signup_tb.email,signup_tb.profilepicture,
allposts_tb.expand   
FROM allposts_tb,friendship_tb,signup_tb
WHERE allposts_tb.name = friendship_tb.newfriend
 and friendship_tb.myname=colname and
 allposts_tb.name=signup_tb.email
and colname=$_SESSION['MM_Username']

UNION ALL

SELECT response_tb.all_id,response_tb.name,response_tb.response, response_tb.time,
friendship_tb.myname,friendship_tb.newfriend,signup_tb.firstname,
signup_tb.lastname,signup_tb.email,signup_tb.profilepicture,
response_tb.expand   
FROM response_tb,friendship_tb,signup_tb
WHERE response_tb.name = friendship_tb.newfriend
 and friendship_tb.myname=colname and
 response_tb.name=signup_tb.email
and colname=$_SESSION['MM_Username']
ORDER BY all_id DESC, time desc

关于mysql - 使用 Sql 子查询显示帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12645439/

相关文章:

php - 使用 PHP/MySQL 客户端从在线无缝过渡到离线?

mysql - LimeSurvey 使用数据库中的 JSON 字符串

Mysql - 来自多个连接表的 'Select max' 没有返回正确的值

mysql - SQL:使用同一张表核对数量

mysql - 如何在 MySQL 中进行备份?

Javascript 正在重新加载时执行

MySQL FROM语句错误: FROM is not a valid input at this position

php - 收集大量访问统计数据的方法

mysql - Yii 在数据库中存储乌尔都语或阿拉伯语单词

javascript - 如何从 bigquery API 查询 bigquery View