简体   繁体   English

尝试联接表时出现MySQL子查询错误(建立评级系统)

[英]Mysql subquery errors when trying to join tables (making rating system)

Ok, here is the problem I am having. 好的,这是我遇到的问题。 There are two tables for handling blog comments and ratings of those comments. 有两个表用于处理博客评论和这些评论的评级。 Each time a logged in user rates a post it inserts a new row in the table 'commentrate'. 每次登录的用户对帖子进行评分时,都会在表“ commentrate”中插入新行。 'commentrate' has 4 fields. “ commentrate”具有4个字段。 Id that is set to auto_increment, author_id, comment_id (the id number of the comment that was rated), and the rating itself on a 1 to 5 scale. 将ID设置为auto_increment,author_id,comment_id(已评分的评论的ID号)以及评分本身(以1到5为单位)。

The table for the actual comments has a commentid field that will match comment_id in the commentrate table. 实际注释表具有一个commentid字段,该字段将与commentrate表中的comment_id匹配。

What I need is a Mysql query that will take each comment in the blog comments table using its commentid and match it to an average of all ratings that have the same comment_id. 我需要一个Mysql查询,该查询将使用其commentid获取博客评论表中的每个评论,并将其与所有具有相同comment_id的评分的平均值进行匹配。

The purpose of this is to build a top rated posts page. 目的是建立一个评分最高的帖子页面。

I have a query that if you specify the comment_id it will return a result set that has the comment_id and an average of the rating. 我有一个查询,如果您指定comment_id,它将返回一个包含comment_id和评分平均值的结果集。 However, I cannot make this query work for each commentid in the blog comments table. 但是,我无法使此查询适用于博客评论表中的每个commentid。 That query is : 该查询是:

SELECT commentrate.comment_id, AVG(commentrate.rating) from commentrate WHERE commentrate.comment_id=35

I have tried to use subqueries and joins to make this happen and it does not work or will only return one row or will give me an error message. 我试图使用子查询和联接来使这种情况发生,它不起作用,或者只会返回一行,或者会给我一条错误消息。 The queries I have tried I will post below. 我尝试过的查询将在下面发布。 Any help would be appreciated. 任何帮助,将不胜感激。 I have struggled in vain for days to figure this out. 为了解决这个问题,我已经白费了几天的努力。 Thank You. 谢谢。

SELECT bd_comments.commentid, bd_comments.comment FROM bd_comments WHERE bd_comments.commentid = ALL (SELECT commentrate.comment_id, floor(AVG(commentrate.rating)) from commentrate WHERE commentrate.comment_id=bd_comments.commentid)

Error: #1241 - Operand should contain 1 column(s) 错误:#1241-操作数应包含1列

SELECT bd_comments.commentid, bd_comments.comment FROM bd_comments WHERE bd_comments.commentid = ALL (SELECT commentrate.comment_id, floor(AVG(commentrate.rating)) from commentrate WHERE commentrate.comment_id=bd_comments.commentid)

Error: #1241 - Operand should contain 1 column(s) 错误:#1241-操作数应包含1列

I think you just want to join the tables together and take the average: 我认为您只想将表连接在一起并取平均值:

select bd.commentid, floor(avg(cr.rating))
from commentrate cr join
     bd_comments c
     on cr.comment_id = bd.commentid
group by bd.commentid;

If you can have duplicate comments in the blog comments table, then you might want: 如果博客评论表中可以有重复的评论,则可能需要:

select cr.comment_id, floor(avg(cr.rating))
from commentrate cr
where cr.comment_id in (select bd.commentid from bd_comments);

Received this answer and it works perfectly: 收到此答案,它可以完美运行:

CREATE TABLE bd_comments ( commentid int , comment varchar(10), author_id int ); 创建表bd_comments(commentid int,comment varchar(10),author_id int);

CREATE TABLE commentrate ( comment_id int , rating int, author_id int ); 创建表注释率(comment_id int,rating int,author_id int);

INSERT INTO bd_comments VALUES (1, "comment 1", 100), (2, "comment 2", 200), (3, "comment 3", 300); 插入bd_comments值(1,“ comment 1”,100),(2,“ comment 2”,200),(3,“ comment 3”,300);

INSERT INTO commentrate VALUES (1, 3.5, 100), (2, 4, 100), (3, 5, 100), (1, 2.5, 200), (2, 1, 200);Here is the query 插入注释值(1,3.5,100),(2,4,100),(3,5,100),(1,2.5,200),(2,1,200);这是查询

SELECT cr.comment_id, floor(avg(cr.rating)) rating,c.comment, c.author_id FROM commentrate cr, bd_comments c WHERE cr.comment_id = c.commentid GROUP BY cr.comment_idOutput 选择cr.comment_id,floor(avg(cr.rating))等级,c.comment,c.author_id从评论率cr,bd_comments c其中,cr.comment_id = c.commentid GROUP BY cr.comment_idOutput

| | COMMENT_ID | COMMENT_ID | RATING | 评分| COMMENT | 评论| AUTHOR_ID | AUTHOR_ID | |------------|--------|-----------|-----------| | ------------ | -------- | ----------- | ----------- | | | 1 | 1 | 3 | 3 | comment 1 | 评论1 | 100 | 100 | | | 2 | 2 | 2 | 2 | comment 2 | 评论2 | 200 | 200 | | | 3 | 3 | 5 | 5 | comment 3 | 评论3 | 300 | 300 |

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

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