Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试连接表时出现Mysql子查询错误(创建分级系统)_Php_Mysql_Subquery_System_Rating - Fatal编程技术网

Php 尝试连接表时出现Mysql子查询错误(创建分级系统)

Php 尝试连接表时出现Mysql子查询错误(创建分级系统),php,mysql,subquery,system,rating,Php,Mysql,Subquery,System,Rating,好的,这是我遇到的问题。有两个表格用于处理博客评论和这些评论的评级。每次登录用户对帖子进行评级时,它都会在“commentrate”表中插入一个新行commentrate'有4个字段。设置为自动递增的Id、作者Id、评语Id评语的Id编号,以及1到5分制的评语本身 实际注释的表有一个commentid字段,该字段将与commentrate表中的注释id匹配 我需要的是一个Mysql查询,它将使用博客评论表中的每个评论的commentid,并将其与具有相同评论id的所有评分的平均值相匹配 这样做的

好的,这是我遇到的问题。有两个表格用于处理博客评论和这些评论的评级。每次登录用户对帖子进行评级时,它都会在“commentrate”表中插入一个新行commentrate'有4个字段。设置为自动递增的Id、作者Id、评语Id评语的Id编号,以及1到5分制的评语本身

实际注释的表有一个commentid字段,该字段将与commentrate表中的注释id匹配

我需要的是一个Mysql查询,它将使用博客评论表中的每个评论的commentid,并将其与具有相同评论id的所有评分的平均值相匹配

这样做的目的是建立一个排名靠前的帖子页面

我有一个查询,如果您指定comment\u id,它将返回一个结果集,该结果集具有comment\u id和评级的平均值。但是,我无法对blog comments表中的每个commentid进行此查询。这个问题是:

SELECT commentrate.comment_id, AVG(commentrate.rating) from commentrate WHERE commentrate.comment_id=35
我已经尝试使用子查询和联接来实现这一点,但它不起作用,或者只返回一行,或者会给我一条错误消息。我尝试过的问题将在下面发布。任何帮助都将不胜感激。我已经徒劳地挣扎了好几天想弄明白这一点。多谢各位

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)
错误: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)

错误:1241-操作数应包含1列

我认为您只需要将表连接在一起并取平均值:

select bd.commentid, floor(avg(cr.rating))
from commentrate cr join
     bd_comments c
     on cr.comment_id = bd.commentid
group by bd.commentid;
如果在博客评论表中可以有重复的评论,那么您可能需要:

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

收到了这个答案,它工作得非常完美:

创建表bd_注释 注释ID int, 评论varchar10, 作者id int ;

创建表注释率 注释_id int, 评级int, 作者id int ;

插入bd_注释 价值观 1,评论1100, 2,评论2200, 意见3300

插入注释率 价值观 1, 3.5, 100, 2, 4, 100, 3, 5, 100, 1, 2.5, 200, 2, 1, 200;这是一个问题

选择cr.comment\u id、flooravgcr.rating rating、c.comment、c.author\u id 从commentrate cr、bd_comments c 其中cr.comment\u id=c.commentid 按cr分组。注释\u i输出

|评论| ID |评级|评论|作者| ID| |------|----|------|------| |1 | 3 |评论1 | 100| |2 | 2 |评论2 | 200|
|3 | 5 | comment 3 | 300 |

commentrate的表格结构如下:id author _idcomment _idrating 23 1 35 5 24 3 35 2 25 3 36 4正如您在最后两行中看到的,多个不同的作者可以对同一条评论进行评级。bd_评论的表格结构如下:commentid postid author_id comment created 35 25 1 Well..2013-08-28 18:49:25 36 25 3 eergerg 2013-08-30 08:46:06我的目标是创建一个表格,跟踪平均评分,并包含bd_评论中每个唯一commentid的评论和作者id。下面是一个我想让MySQL做的例子:commentid postid author_id comment创建了avgrating 35 25 1 Well。。时间戳4 36 26 2注释时间戳4对于每个commentid,将平均评级加入表的末尾。对于bd_comments中的每个commentid,我都需要这个avgrating。换句话说,上面示例中的avgrating将通过以下方式返回:SELECT flooravgcomcentrate.rating,其中bd_comments.commentid=35此语句选择特定commentid的所有评级的平均值,我需要为每个唯一的bd_comments.commentid返回commentrate.rating的平均值,并将其连接到正确的bd_comments行,这样我就可以得到bd_comments中所有commentid的上述结果。谢谢你在这个问题上的帮助。