gpt4 book ai didi

mysql - 如何从表中获取 count_id 和 rating_evarate?

转载 作者:行者123 更新时间:2023-11-29 06:47:06 25 4
gpt4 key购买 nike

如何从表中获取count_id和rating_evarate?

我想计算 ID 并从下表中获取评分。
我怎样才能在 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

最佳答案

你可以试试这个:

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

参见this SQLFiddle

关于mysql - 如何从表中获取 count_id 和 rating_evarate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18029373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com