简体   繁体   English

用于联接包含多个行的多个表的SQL查询-正确的方法吗?

[英]SQL Query for joining multiple tables containing multiple rows - The right way?

I have a situation where I have 4 tables of following schema : 我遇到以下情况的4个表的情况:

#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 = 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. 由于一个用户可以有多个帖子,因此多个post_id与一个用户ID相关。 A post can contain multiple replies, so multiple reply_id's are related to a single post. 一个帖子可以包含多个回复,因此多个reply_id与一个帖子相关。 A reply can contain multiple photos, so multiple photo_id's are related to a single reply. 一个回复可以包含多张照片,因此多个photo_id与一个回复相关。

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? 如果这是结果的结果,那么如何在没有太大问题的情况下遍历php所有字段?

Thank you in advance. 先感谢您。

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. 但是,在这种情况下,您可以使用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 ~. 然后,您可以展开出ReplyPhotoId字段(默认情况下为逗号),然后针对该结果数组中的每个元素,根据〜再次使其展开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM