查询链接4个表

时间:2015-09-15 10:20:03

标签: mysql sql join inner-join

目前我有4张桌子:

用户:

  • UserID (PK)
  • FName参数
  • SNAME
  • 产品图

文章:

  • ReportID (PK)
  • 用户ID
  • 标题
  • 描述
  • 产品图

注释:

  • CommentID (PK)
  • 用户ID
  • 帖子ID

UserSubscriptions:

  • ID (PK)
  • 用户ID
  • 帖子ID
  • isRead

我需要检索用户订阅的所有帖子的所有评论详细信息(有可用的评论)。此查询用于填充应用中的我的活动供稿页面。

当用户发帖时,他们会订阅该帖子,当用户对帖子发表评论时,他们会订阅该帖子。所以' AND'查询中WHERE的一部分是确保某人对用户订阅的帖子发表了评论。因此,如果没有人对此帖进行评论,作者不应该收到关于他们所撰写的帖子的回复。所以我的想法是,用户a会收到一个回复​​,例如用户b已对帖子c评论了标题xxx'。

我的查询问题是我没有从userSubscriptions表中收到正确的isRead Val。

我的尝试:

$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小提琴:http://sqlfiddle.com/#!9/9aa16

小提琴返回1行,但我在phpmyadmin中的查询返回两个正确的行,但isRead值不正确。

当传入userID 36时,我正在返回两个正确的行但每行中的isRead col是错误的,因此在连接到userSubscriptions表时出现了问题

注意: 当我在查询中包含userSubscriptions.ID时,我可以看到我得到的4和16是不正确的行,并且它们的isRead值为1.但是对于所有帖子atm,用户36的isRead值为0。

修改: 我已经修改了查询,它似乎正在运行。如果有人发现问题,请指出。我已经添加了一个并进入第一个内部联接:

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

2 个答案:

答案 0 :(得分:2)

也许你混淆了外键。

INNER JOIN Posts ON userSubscriptions.PostID = Posts.ReportID

为此,PostID必须是Posts.ReportID

的外键

查看UserSubscriptions.PostID的FOREIGN KEY定义,找出它链接到哪个表。

答案 1 :(得分:0)

尝试以下查询 -

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;

另请查看此处 - http://sqlfiddle.com/#!9/9aa16/6