1

如何从表中获取 count_id 和 rating_evarate?

我想计算 id 并从下表中获得 rating evarate 。
我怎么能在 mysql 中做到这一点。

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `rating`
-- ----------------------------
DROP TABLE IF EXISTS `rating`;
CREATE TABLE `rating` (
  `id` int(11) NOT NULL,
  `rating` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of rating
-- ----------------------------
INSERT INTO `rating` VALUES ('1', '5');
INSERT INTO `rating` VALUES ('2', '0');
INSERT INTO `rating` VALUES ('3', '0');
INSERT INTO `rating` VALUES ('4', '0');
INSERT INTO `rating` VALUES ('2', '0');
INSERT INTO `rating` VALUES ('1', '0');

在此处输入图像描述

4

2 回答 2

1

你可以试试这个查询:

SELECT id, count_id, AVG(rating_average) FROM rating GROUP BY count_id;
于 2013-08-03T04:57:02.267 回答
1

你可以试试这个:

SELECT ID
     , COUNT(ID) Count_ID
     , AVG(Rating) AS rating_average
FROM rating
GROUP BY ID;

看到这个 SQLFiddle

于 2013-08-03T04:57:47.157 回答