duanlushen8940 2014-03-29 21:13
浏览 89
已采纳

SQL Query用于连接包含多行的多个表 - 正确的方法?

I have a situation where I have 4 tables of following schema :

#1 : Table USER
user_id | user_details....

#2 : Table POSTS
post_id | user_id | post_details....

#3 : Table REPLIES
reply_id | post_id | by_user_id | reply_details...

#4 : Tables REPLY_PHOTOS
photo_id | reply_id | photo_details...

I want to retrieve all the posts of a particular user along with the replies on them and the photos in the replies. Every usage of columns from the previous table is in foreign key constraint.

SELECT USER.user_id, POST.post_id, REPLIES.reply_id, REPLY_PHOTOS.photo_id
FROM USER WHERE USER.user_id = 1
LEFT JOIN POST on USER.user_id = POST.user_id
LEFT JOIN REPLIES on POST.post_id = REPLIES.post_id
LEFT JOIN REPLY_PHOTOS on REPLIES.reply_id = REPLY_PHOTOS.reply_id;

The problem I'm facing is, when I am using joins normally I am getting rows that I suspect is not good practice.

Example of result I'm getting for user_id = 1 :

user_id | post_id | reply_id | photo_id
--------Post 1 - Reply 1 (Repeat for photos)----------------
   1         23        17         26
   1         23        17         32
   1         23        17         47
-------Post 1 - Reply 2 (Repeat for its photos)-----------
   1         23        34         12
   1         23        34         18
   1         23        34         23
   1         23        34         31
------Post 1 - Reply 3 (Contains no photos)------------
   1         23        41         null
---------Next Post for same user (contd.)----------------

As a user can have multiple posts, so multiple post_id's are related to a single user id. A post can contain multiple replies, so multiple reply_id's are related to a single post. A reply can contain multiple photos, so multiple photo_id's are related to a single reply.

Also, I want to further extend it to store Likes which will be another pain in the ass.

I don't know if my schema needs to be change (if yes, suggest me a better way) or if I'm not able to understand the basics of join.

If this is the way results should be, then how can I traverse to all the fields in php without much issues?

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • doubailian4459 2014-03-29 21:58
    关注

    Most of the time I would quite happily process the duplicate parts of the returned rows, ignoring them. However in this situation you could get away with using GROUP_CONCAT.

    Something like this:-

    SELECT USER.user_id, POST.post_id, GROUP_CONCAT(CONCAT_WS('~',REPLIES.reply_id, REPLY_PHOTOS.photo_id)) AS ReplyPhotoId
    FROM USER WHERE USER.user_id = 1
    LEFT JOIN POST on USER.user_id = POST.user_id
    LEFT JOIN REPLIES on POST.post_id = REPLIES.post_id
    LEFT JOIN REPLY_PHOTOS on REPLIES.reply_id = REPLY_PHOTOS.reply_id
    GROUP BY USER.user_id, POST.post_id
    

    Then you can explode out the ReplyPhotoId field (on the commas by default), and for each element of that resulting array explode it again based on ~.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料