[英]How to get Average using “Group By” in mysql php
我在mysql中使用“GroupBy”獲得“Average”
id config_id question rating
1 68 lorem1 3
2 68 lorem2 5
3 69 lorem3 5
4 68 lorem1 NULL
我想獲得每個“config_id”的平均評分(不重復)我嘗試使用以下代碼,但對我不起作用
SELECT AVG(rating) AS AVERAGE
FROM (SELECT config_id, AVG(rating) AS avg_rating
FROM "nps_ans" GROUP BY config_id) A
;
SELECT AVG(rating) AS AVERAGE
FROM nps_ans GROUP BY config_id;
如果您需要與問題無關的每個 config_id 的平均值,
select config_id,AVG(rating) AS average from nps_ans group by config_id;
要么
select question,config_id,AVG(rating) AS average from nps_ans group by config_id,question;
如果您需要每個配置和每個問題的平均評分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.