mingfish_004
mingfish_004

Reputation: 1413

How to get count_id and rating_evarate from table?

How to get count_id and rating_evarate from table?

I want to count the id and get rating evarate from the table bellow .
how can I do it in 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');

enter image description here

Upvotes: 1

Views: 42

Answers (2)

Himanshu
Himanshu

Reputation: 32602

You can try this:

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

See this SQLFiddle

Upvotes: 1

Aviel Fedida
Aviel Fedida

Reputation: 4102

you can try this query:

SELECT id, count_id, AVG(rating_average) FROM rating GROUP BY count_id;

Upvotes: 1

Related Questions