SQL平均多列

时间:2015-01-05 17:03:54

标签: php sql average rows

你将如何计算多列和行的平均值?

到目前为止我的代码是:

SELECT b.id, r.id, b.name, r.business_id, (r.service + r.value + r.quality) / 3 AS average 
                        FROM business b
                        LEFT JOIN rating r ON r.business_id = b.id

enter image description here

我想得到r.service,r.value和r.quality的平均值,并结合business_id列。

所以我应该得到: business_id = average:9.42(例如)

1 个答案:

答案 0 :(得分:3)

聚合函数AVG应该采用表达式进行聚合,因此这应该有效:

SELECT b.name, r.business_id, AVG((r.service + r.value + r.quality)/3)
FROM business b
    LEFT JOIN rating r ON r.business_id = b.id
GROUP BY b.name, r.business_id
LIMIT 0,30