mysql 查詢所有評論以及回復(fù)
問題描述
表設(shè)計(jì)如下
question(id, user_id,content)answer (id,question_id,user_id,content)reply (id,answer_id, user_id,content)
請問要怎么一次性查詢所有的評論和回復(fù)啊目前用group by和group_concat實(shí)現(xiàn)不了額..
問題解答
回答1:我理解:question是問題表,羅列所有的提問answer 是回復(fù)表,對某個(gè)具體問題的回復(fù),用question_id與question表中的id做關(guān)聯(lián)reply 是對某個(gè)回復(fù)的評論,用answer_id與answer表中的id做關(guān)聯(lián)以下代碼,基于以上理解
select t1.q_id as 問題id, t1.q_user_id as 提問者id, t1.q_content as 問題內(nèi)容, t2.a_id as 回復(fù)id, t2.a_user_id as 回復(fù)者id, t2.a_content as 回復(fù)內(nèi)容, t3.r_id as 評論id, t3.r_user_id as 評論者id, t3.r_content as 評論內(nèi)容from (select id as q_id ,user_id as q_user_id ,content as q_contentfrom question) t1 -- 所有的問題列表,用id做唯一性的區(qū)分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 -- 每個(gè)question_id對應(yīng)的回復(fù)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 -- 每個(gè)answer_id對應(yīng)的評論回答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)
相關(guān)文章:
1. javascript - requestAnimationFrame如何控制幀速?2. java - com.android.internal.R.attr.dialogTheme 這個(gè)dialogTheme的內(nèi)容再哪里查看?3. java - 關(guān)于aop在controller不起用的問題4. android glide asbitmap 在baseadpter中的問題5. mysql 怎么做到update只更新一行數(shù)據(jù)?6. java如何高效讀寫10G以上大文件7. mysql - 在log日志中已知用戶的某一步操作,如何獲取其上一步操作?8. javascript - immutable配合react提升性能?9. php對mysql提取數(shù)據(jù)那種速度更快10. mysql事務(wù)回滾定位

網(wǎng)公網(wǎng)安備