您好,登录后才能下订单哦!
DROP TABLE IF EXISTS `t_user_evaluate`; CREATE TABLE `t_user_evaluate` ( `id` int(25) NOT NULL AUTO_INCREMENT COMMENT '主键ID', `matron_id` int(25) NOT NULL COMMENT 'matron_id', `order_id` int(25) NOT NULL COMMENT '订单t_matron_order.id', `file_id` int(10) DEFAULT NULL COMMENT '图片id关联t_system_files.id', `evaluate_type` char(10) DEFAULT NULL COMMENT '1.宝宝护理 2 沟通技巧 3膳食搭配 4服务态度', `score_grade` int(11) DEFAULT NULL COMMENT '技能评分(满意等级,1:一星非常不满意,2:二星不满意:3:三星一般满意,4:四星比较满意,5:五星非常满意)', `synthesize_grade` int(11) DEFAULT NULL COMMENT '综合评分(1:一星非常不满意,2:二星不满意:3:三星一般满意,4:四星比较满意,5:五星非常满意)', `phone` varchar(11) NOT NULL COMMENT '手机号码,匿名时加星号存储', `context` varchar(200) DEFAULT NULL COMMENT '评价内容', `status` char(1) NOT NULL DEFAULT '0' COMMENT '状态,0:未删除,1:已删除', `create_time` datetime NOT NULL COMMENT '创建时间', `update_id` varchar(25) DEFAULT '' COMMENT '修改人', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`), KEY `idx_u_user_id` (`user_id`) USING BTREE, KEY `idx_um_matron_id` (`matron_id`) USING BTREE, KEY `idx_o_order_id` (`order_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='评价表'; -- ---------------------------- -- Records of t_user_evaluate -- ---------------------------- INSERT INTO `t_user_evaluate` VALUES ('1', '1', '11', '10001', null, '1', '3', '5', '111', null, '0', '2018-04-30 15:42:00', '', null); INSERT INTO `t_user_evaluate` VALUES ('2', '1', '11', '10001', null, '2', '4', '5', '111', null, '0', '2018-04-30 15:42:56', '', null); INSERT INTO `t_user_evaluate` VALUES ('3', '1', '11', '10001', null, '3', '4', '5', '111', null, '0', '2018-04-30 15:44:16', '', null); INSERT INTO `t_user_evaluate` VALUES ('4', '1', '11', '10001', null, '4', '4', '5', '111', null, '0', '2018-04-30 15:45:18', '', null); INSERT INTO `t_user_evaluate` VALUES ('5', '1', '11', '10002', null, '1', '3', '4', '111', null, '0', '2018-04-30 15:42:00', '', null); INSERT INTO `t_user_evaluate` VALUES ('6', '1', '11', '10002', null, '2', '3', '4', '111', null, '0', '2018-04-30 15:42:56', '', null); INSERT INTO `t_user_evaluate` VALUES ('7', '1', '11', '10002', null, '3', '3', '4', '111', null, '0', '2018-04-30 15:44:16', '', null); INSERT INTO `t_user_evaluate` VALUES ('8', '1', '11', '10002', null, '4', '3', '4', '111', null, '0', '2018-04-30 15:45:18', '', null);
###################################################################
业务需求:根据评价类别(evaluate_type)计算技能评分(score_grade)平均分
#######################普通sql##############################################
SELECT AVG(t.score_grade) as A FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=1 ;
SELECT AVG(t.score_grade) as B FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=2 ;
SELECT AVG(t.score_grade) as C FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=3 ;
SELECT AVG(t.score_grade) as D FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=4 ;
######################使用union ALL####################
SELECT AVG(t.score_grade) as A FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=1
UNION ALL
SELECT AVG(t.score_grade) as B FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=2
UNION ALL
SELECT AVG(t.score_grade) as C FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=3
UNION ALL
SELECT AVG(t.score_grade) as D FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=4
#####################使用子查询######################
SELECT DISTINCT
(SELECT AVG(t.score_grade) FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=1 )as a,
(SELECT AVG(t.score_grade) FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=2) as b,
(SELECT AVG(t.score_grade) FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=3 ) as c,
(SELECT AVG(t.score_grade) FROM `t_user_evaluate` t where t.matron_id=11 AND t.evaluate_type=4 ) as d
FROM `t_user_evaluate`
##################使用CASE WHEN 条件 THEN 值 END ########################
SELECT AVG(CASE WHEN t.evaluate_type=1 THEN t.score_grade end) as a,
AVG(CASE WHEN t.evaluate_type=2 THEN t.score_grade end) as b,
AVG(CASE WHEN t.evaluate_type=3 THEN t.score_grade end) as c,
AVG(CASE WHEN t.evaluate_type=4 THEN t.score_grade end) as d
FROM `t_user_evaluate` t where t.matron_id=11 ;
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。