Mysql分段查詢數量及平均分(總結)
阿新 • • 發佈:2018-12-09
SELECT
target_id,
count(0) comment_total_num,
format((avg(content_score) + avg(effect_score) + avg(method_score)) / 3.0,1) course_total_score,
COUNT(CASE WHEN c.content_score=5 THEN c.id END) five_star,
COUNT(CASE WHEN c.content_score=4 THEN c.id END) four_star,
COUNT(CASE WHEN c.content_score=3 THEN c.id END) three_star,
COUNT(CASE WHEN c.content_score=2 THEN c.id END) two_star,
COUNT(CASE WHEN c.content_score=1 THEN c.id END) one_star
FROM
course_comment c
WHERE
c.delete_flag = 0 and c.target_id=#{courseId}