简体   繁体   中英

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' has 4 fields. 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.

The table for the actual comments has a commentid field that will match comment_id in the commentrate table.

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.

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. However, I cannot make this query work for each commentid in the blog comments table. 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)

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)

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

CREATE TABLE commentrate ( 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);

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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