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 ~.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?