casewhen
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 ;
分享文章:casewhen
网址分享:http://ybzwz.com/article/pccped.html