我已经搜索类似的网站和所有的答案是指一个单独的评级表,而不是像这样的实现。平均评级与评级存储在2列
我已经转换一个WordPress网站在PHP/MySQL的一个自建的网站,想象实际的岗位是一个审查和评论是与原来的岗位实际的评论。
我遇到的问题是我的职位表和评价领域的分级字段为每个评论输入。
我试图使用别名AvgRating作为一个,但它只返回评论评级(c.rating),如果该行被删除它从posts表中带回评级,有没有加入方式别名作为平均评分?显然只有这部分是错误的,但不会抛出错误。
SELECT p.post_id
, p.title
, AVG(p.rating) AvgRating
, AVG(c.rating) AvgRating -- returns average from comments field, if line is removed will return posts rating value instead..
FROM posts p
LEFT
JOIN comments c
ON p.post_id = c.post_id
WHERE p.post_id = $post_id
AND c.post_id = $post_id
AND active = 1
GROUP
BY p.post_id
, p.title;
我在尝试获取模式聚合计数标记的平均值。有一种方法可以在一个查询中实现此目的吗?或者是我唯一的选择使用2个查询返回2个平均值,并使用PHP函数返回两者之间的平均值?
这是一个测试数据库:
CREATE TABLE `posts` (
`post_id` int(11) NOT NULL,
`cat_id` int(11) NOT NULL,
`rating` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`content` varchar(5000) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`author` varchar(255) NOT NULL,
`active` int(11) DEFAULT '0',
`slug` varchar(255) NOT NULL,
`user_submitted` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=2157 DEFAULT CHARSET=latin1;
CREATE TABLE `comments` (
`cmt_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`rating` int(11) NOT NULL,
`comment` varchar(5000) NOT NULL,
`cmt_author` varchar(255) NOT NULL,
`cmt_email` varchar(255) NOT NULL,
`ip_address` varchar(100) NOT NULL,
`cmt_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cmt_status` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=397 DEFAULT CHARSET=latin1;
INSERT INTO `posts` (`post_id`, `cat_id`, `rating`, `title`, `content`, `date_added`, `author`, `active`, `slug`, `user_submitted`) VALUES
(2, 9, 5, 'site1.com', 'Original post review body', '2010-05-07 05:00:00', '', 1, 'site1-com', 1);
INSERT INTO `comments` (`cmt_id`, `post_id`, `rating`, `comment`, `cmt_author`, `cmt_email`, `ip_address`, `cmt_date`, `cmt_status`) VALUES
(1, 2, 5, 'it is a good site test review', 'Anonymous', '', '', '2010-01-06 21:51:00', 1),
(2, 2, 3, 'it is an average site test review', 'Anonymous', '', '', '2010-01-06 20:51:00', 1),
(3, 2, 1, 'it is an bad site test review', 'Anonymous', '', '', '2010-01-06 23:51:00', 1),
(4, 2, 0, 'neutral test review', 'Anonymous', '', '', '2010-01-06 22:51:00', 1),
(5, 2, 2, 'below average test review', 'Anonymous', '', '', '2010-01-06 22:51:00', 1);
查询运行:
SELECT p.post_id
, p.title
, AVG(p.rating) AvgRating
, AVG(c.rating) AvgRating -- returns average from comments field, if line is removed will return posts rating value instead..
FROM posts p
LEFT
JOIN comments c
ON p.post_id = c.post_id
WHERE p.post_id = 2
AND active = 1
GROUP
BY p.post_id
, p.title;
上测试数据库恢复运行此在phpMyAdmin:AvgRating:当前选择不包含唯一的列。电网编辑,复选框,编辑,拷贝和删除功能不可用
(这是可以预料的,因为我错误地认为这将令两者的平均值为别名列avgRating的一个实例)
结果:
post_id title AvgRating AvgRating
2 site1.com 5.0000 2.2000
所以这是正确的在一定意义上,而是如何在原岗位等级的5.000到的评论的评价2.2000因素? (5 + 5 + 3 + 1 + 0 + 2)/6=2.66667,期望的结果应该是2.6667而不是2.2000。 2.2000是唯一的评论评分的平均值,并在原岗位等级的5.000
我错过了什么?这似乎太明显了。两个别名都是一样的。所以在PHP中,第一个别名被第二个别名覆盖。 – Strawberry
另外'AND c.post_id = $ post_id'是多余的(事实上,这会产生反作用,因为它会将LEFT [OUTER] JOIN渲染为INNER JOIN)。 – Strawberry
嗨,是的,对不起,我意识到发生了这种情况,我努力从两个表中获得平均值,平均值来自评论评级领域,这很好,但需要考虑来自帖子评级领域的原始评级,并以某种方式将其添加到整体平均值。 – Chris