[英]Mysql user subscription query fails
I have a database named socialnetwork and has 5 tables category
, post_category
, posts
, subscribe
, user
. 我有一个名为socialnetwork的数据库,并具有5个表
category
,即post_category
, posts
, subscribe
和user
。
my table structures 我的桌子结构
-------- -------------
category posts
-------- ------------
categoryID postID
categoryName post
userID
categoryID
-------------- ---------------
post_category subscribe
--------------- ---------------
postID subscriberID
categoryID categoryID
--------------
usertable
--------------
userID
userName
data's in the table 表格中的数据
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
User 1 can subscribe to different categories and he can see the articles in the categories he subscribes. 用户1可以订阅不同的类别,并且他可以看到他所订阅的类别中的文章。 That's what i am trying to implement here.
这就是我在这里要实现的。 And i have this query but it's not giving me the result i want.
我有这个查询,但它没有给我我想要的结果。
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
Here's the result i am getting 这是我得到的结果
---------------------------------
username post category
----------------------------------
jijojohn32 this post is cool film, film
sijojohn demo post television
The result i want 我想要的结果
---------------------------------------------
username post category
-------------------------------------------
jijojohn32 this post is cool film,television
sijojohn demo post television
I want the post from the categories he subscribed to , the username of the user posted the articles , and categories which posts reside. 我想要他订阅的类别中的帖子,发布文章的用户的用户名以及帖子所驻留的类别。 What's wrong in my query ?.
我的查询出了什么问题? any idea ?.
任何想法 ?。 thanks
谢谢
You are not aggregating by the right columns. 您未按右列进行汇总。 I think this is the query that you want:
我认为这是您想要的查询:
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;
There exists a conflict 存在冲突
Category table consists of
类别表包括
Category id and Category Name
Post Table consists of
帖子表包括
Post id and Corresponding Category Id
As well as 以及
Post_Category table consists of
Post_Category表包含
Post id and Category Id
Hence it is picking from the Posts table
, that Row 1 - has only 1 category id
associated with it 因此,它是从
Posts table
,第1行-仅具有与其关联的1个category id
I suggest you remove Category id
from Post
table 我建议您从
Post
表中删除Category id
It is pointless to have 2 keys in 1 table, and similar 2 keys in other table. 在一个表中有2个键,而在其他表中具有类似的2个键是没有意义的。
Try to establish Primary key Foreign key relationship.
尝试建立主键外键关系。
Hope that helps 希望能有所帮助
Here's the working query. 这是工作查询。 I made some modifications in the table.
我在表中做了一些修改。 Deleted the
categoryID
from posts. 从帖子中删除了
categoryID
。 Made a new table called post_category. 制作了一个名为post_category的新表。
--------------
post_category
-------------
postID
categoryID
Here's the query 这是查询
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.