query linking 4 tables

走远了吗. 提交于 2019-12-14 04:00:00

问题


I have 4 tables at present:

Users:

  • UserID (PK)
  • FName
  • SName
  • Pic

Posts:

  • ReportID (PK)
  • UserID
  • Title
  • Description
  • Pic

Comments:

  • CommentID (PK)
  • UserID
  • PostID

UserSubscriptions:

  • ID (PK)
  • UserID
  • PostID
  • isRead

I need to retrieve all comment details for all posts a user is subscribed to (where there is a comment available). This query is used to populate a my activity feed page in an app.

When a user makes a post, they are subscribed to that post and when a user comments on a post, they are subscribed to that post. So the reason for the 'AND' part of the WHERE in the query is to ensure that someone has commented on a post a user is subscribed to. So the author should not receive a response with a row about a post they have written if no one has commented on it yet. so the idea is that user a will receive a response like 'user b has commented on post c with title xxx'.

The issue with my query is that I am not receiving the correct isRead Val from the userSubscriptions table.

My attempt:

$ReportDetails_Query = "SELECT Posts.ReportID, Posts.Title AS postTitle, Posts.Pic as postPic, Posts.Description as postDescription,
            Posts.datePosted AS postDatePosted, Posts.photoWidth as postPhotoWidth, posts.photoHeight as postPhotoHeight, Users.FName as authorFname,
            Users.SName as authorSname, Users.Pic as postAuthorPic, Users.UserID As authorID, userSubscriptions.isRead
            FROM UserSubscriptions 
            INNER JOIN Posts ON userSubscriptions.PostID = Posts.ReportID
            INNER JOIN Users ON Posts.UserID = Users.UserID  
            WHERE Posts.ReportID IN 
                                (SELECT PostID FROM UserSubscriptions WHERE UserID = ?) 
            AND 
                (SELECT COUNT(CommentID) 
                FROM Comments WHERE UserID <> ? 
                AND Comments.ReportID = Posts.ReportID) > 0                         
            GROUP BY Posts.ReportID
            LIMIT ?,10";

SQL Fiddle: http://sqlfiddle.com/#!9/9aa16

The fiddle is returning 1 row but my query here in phpmyadmin returns two correct rows but with incorrect isRead vals.

When passing in the userID 36, I am getting back the two correct rows but the isRead col in each row is wrong, so there is something going wrong in the join to the userSubscriptions table

Note: When I include userSubscriptions.ID in the query, I can see I am getting 4 and 16 which are incorrect rows and they have isRead vals of 1. But the isRead vals for user 36 are 0 for all posts atm.

EDIT: I've modified the query and it seems to be working. If anyone sees an issue with it, please point it out. i've added an and into the first inner join:

SELECT Posts.ReportID, Posts.Title AS postTitle, Posts.Pic as postPic, Posts.Description as postDescription,
            Posts.datePosted AS postDatePosted, Posts.photoWidth as postPhotoWidth, posts.photoHeight as postPhotoHeight, Users.FName as authorFname,
            Users.SName as authorSname, Users.Pic as postAuthorPic, Users.UserID As authorID, userSubscriptions.isRead
            FROM UserSubscriptions 
            INNER JOIN Posts ON userSubscriptions.PostID = Posts.ReportID and userSubscriptions.UserID = 36
            INNER JOIN Users ON Posts.UserID = Users.UserID  
            WHERE Posts.ReportID IN 
                                (SELECT PostID FROM UserSubscriptions WHERE UserID = ?) 
            AND 
                (SELECT COUNT(CommentID) 
                FROM Comments WHERE UserID <> ? 
                AND Comments.ReportID = Posts.ReportID) > 0                             
            GROUP BY Posts.ReportID
            LIMIT ?,10

回答1:


Perhaps you have mixed up foreign keys.

INNER JOIN Posts ON userSubscriptions.PostID = Posts.ReportID

For this to work, PostID has to be a foreign key into Posts.ReportID

Look at the FOREIGN KEY definitions for UserSubscriptions.PostID, and find out which table it's linked to.




回答2:


Try below query-

SELECT Posts.ReportID, Posts.Title AS postTitle, Posts.Pic AS postPic, Posts.Description AS postDescription,
            Posts.datePosted AS postDatePosted, Posts.photoWidth AS postPhotoWidth, posts.photoHeight AS postPhotoHeight, Users.FName AS authorFname,
            Users.SName AS authorSname, Users.Pic AS postAuthorPic, Users.UserID AS authorID, userSubscriptions.isRead
            FROM UserSubscriptions 
            INNER JOIN Posts ON userSubscriptions.PostID = Posts.reportid 
            INNER JOIN Users ON Posts.UserID = Users.UserID  
            WHERE UserSubscriptions.UserID = 36
            AND 
                (SELECT COUNT(CommentID) 
                FROM Comments WHERE UserID <> 36 
                AND Comments.ReportID = Posts.ReportID) > 0                         
            GROUP BY Posts.ReportID
            LIMIT 10;

Also check here- http://sqlfiddle.com/#!9/9aa16/6



来源:https://stackoverflow.com/questions/32583553/query-linking-4-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!