2016-07-24 93 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