繁体   English   中英

MySQL用户订阅查询失败

[英]Mysql user subscription query fails

我有一个名为socialnetwork的数据库,并具有5​​个表category ,即post_categorypostssubscribeuser

我的桌子结构

     --------                       -------------
     category                       posts  
     --------                       ------------
     categoryID                     postID 
     categoryName                   post
                                    userID
                                    categoryID

   --------------                ---------------         
    post_category                subscribe
   ---------------               ---------------
    postID                       subscriberID  
    categoryID                   categoryID

  --------------
   usertable
  --------------
   userID
   userName

表格中的数据

category table                              usertable
--------------------------               -------------------
categoryID     categoryName               userID      userName
---------------------------              --------------------
1                film                      1    jijojohn32
2               television                 2    sijojohn                                 


posts_category table                     subscribe table
------------------                      -------------------------
postID     categoryID                    subscriberID    categoryID
---------------------                   ------------------------
1            1                            1                1
1            2                            1                2     
2            2                            2                2                 


 posts table
---------------------------------------------------
 postID       post             userID     categoryID
 --------------------------------------------------
 1         this post is cool    1           1
 2           demo post          2           2

用户1可以订阅不同的类别,并且他可以看到他所订阅的类别中的文章。 这就是我在这里要实现的。 我有这个查询,但它没有给我我想要的结果。

USE socialnetwork;
SELECT  socialnetwork.usertable.userName,socialnetwork.posts.post, GROUP_CONCAT(socialnetwork.category.categoryName) as category
FROM socialnetwork.category
INNER JOIN subscribe
ON subscribe.categoryID = category.categoryID
INNER JOIN posts
ON posts.categoryID = subscribe.categoryID
INNER JOIN usertable
ON usertable.userID = posts.userID
INNER JOIN socialnetwork.post_category
ON post_category.postID = posts.postID

WHERE subscriberID = "1"
GROUP BY socialnetwork.category.categoryName

这是我得到的结果

 ---------------------------------
    username       post            category
    ----------------------------------
   jijojohn32  this post is cool   film, film
    sijojohn   demo post           television

我想要的结果

 ---------------------------------------------
    username       post               category
    -------------------------------------------
   jijojohn32  this post is cool     film,television
    sijojohn   demo post              television

我想要他订阅的类别中的帖子,发布文章的用户的用户名以及帖子所驻留的类别。 我的查询出了什么问题? 任何想法 ?。 谢谢

您未按右列进行汇总。 我认为这是您想要的查询:

SELECT ut.userName, p.post, GROUP_CONCAT(c.categoryName) as category
FROM socialnetwork.category c INNER JOIN
     subscribe s
     ON s.categoryID = c.categoryID INNER JOIN
     posts p
     ON p.categoryID = s.categoryID INNER JOIN
     usertable ut
     ON ut.userID = p.userID INNER JOIN
     socialnetwork.post_category pc
     ON pc.postID = p.postID
WHERE subscriberID = 1
GROUP BY ut.userName, p.post;

存在冲突

类别表包括

Category id and Category Name

帖子表包括

Post id and Corresponding Category Id

以及

Post_Category表包含

Post id and Category Id

因此,它是从Posts table ,第1行-仅具有与其关联的1个category id

我建议您从Post表中删除Category id

在一个表中有2个键,而在其他表中具有类似的2个键是没有意义的。

尝试建立主键外键关系。

希望能有所帮助

这是工作查询。 我在表中做了一些修改。 从帖子中删除了categoryID 制作了一个名为post_category的新表。

--------------
post_category
-------------
postID
categoryID

这是查询

SELECT GROUP_CONCAT(category.categoryName) as category  , category.categoryID , subscribe.subscriberID , posts.post ,
  usertable.userName

from category

INNER JOIN subscribe
ON subscribe.categoryID = category.categoryID


INNER JOIN post_category
ON category.categoryID = post_category.categoryID

INNER JOIN posts
ON posts.postID =  post_category.postID

INNER JOIN usertable
ON usertable.userID = posts.userID

WHERE subscriberID = 1
GROUP BY post_category.postID

暂无
暂无

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

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