2016-07-24 95 views
1

我有一个名为socialnetwork的数据库,并有5个表category,post_category,posts,subscribe,userMysql用户订阅查询失败

我的表结构

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

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

+0

对不起更新:) – user2967888

回答

1

这里的工作查询。我在表格中做了一些修改。从帖子中删除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 
2

您没有通过正确的列汇总。我认为这是你想要的查询:

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; 
+0

都能跟得上它给了相同的结果集。 – user2967888

+0

任何想法我的查询花花公子有什么不对? – user2967888

+0

@ user2967888。 。 。正如我在开头的句子中提到的那样,你不是通过正确的列进行汇总。 –

1

存在一个冲突

分类表由

Category id and Category Name

后表由

Post id and Corresponding Category Id

除了

Post_Category表由

Post id and Category Id

因此,它被从Posts table采摘,即 第1行 - 仅具有1 category id相关联用它

我建议你删除Category idPost

是没有意义的有1和表2把钥匙,而在其他表类似的2个键。

尝试建立主键外键关系。

希望帮助

+0

我已经知道了。 – user2967888

+0

你能告诉我如何重写查询吗? @Chaitanya Bapat – user2967888