mysql 查询所有评论以及回复
问题描述
表设计如下
question(id, user_id,content)answer (id,question_id,user_id,content)reply (id,answer_id, user_id,content)
请问要怎么一次性查询所有的评论和回复啊目前用group by和group_concat实现不了额..
问题解答
回答1:我理解:question是问题表,罗列所有的提问answer 是回复表,对某个具体问题的回复,用question_id与question表中的id做关联reply 是对某个回复的评论,用answer_id与answer表中的id做关联以下代码,基于以上理解
select t1.q_id as 问题id, t1.q_user_id as 提问者id, t1.q_content as 问题内容, t2.a_id as 回复id, t2.a_user_id as 回复者id, t2.a_content as 回复内容, t3.r_id as 评论id, t3.r_user_id as 评论者id, t3.r_content as 评论内容from (select id as q_id ,user_id as q_user_id ,content as q_contentfrom question) t1 -- 所有的问题列表,用id做唯一性的区分left outer join(select id as a_id ,question_id ,user_id as a_user_id ,content as a_contentfrom answer) t2on t1.q_id = t2.question_id -- 每个question_id对应的回复left outer join(select id as r_id ,answer_id ,user_id as r_user_id ,content as r_content) t3 on t2.a_id = t3.answer_id -- 每个answer_id对应的评论回答2:
select reply.,answer.,question.* fromreply right join answer onreply.answer_id = answer.I’dright join question onanswer.question_id = question.idWhere question.id =(查找的question.id)
相关文章:
1. javascript - 引入 simditor,但是显示标签,这个怎么解决。2. docker api 开发的端口怎么获取?3. android - 目前有哪些用Vue.js开发移动App的方案?4. javascript - 仿着echarts官网的地图做了个例子,但是只显示出来了地点,没有画出飞机动态效果??急5. javascript - 关于document.documentElement.scrollTop和documen.body.scrollTop的問題6. java - idea 新建项目提示 already exists in VFS7. calss调用了 也设置了颜色,可是网站不显示。请问大佬是什么情况啊8. python - scrapy获取网页指定内容,后翻到下一页继续,固定循环次数。。问题9. javascript - js写一个递归把数据结构重组成另外的结构10. background属性顺序?