SQL计算字段

时间:2010-08-11 14:55:23

标签: sql mysql calculated-field

我正在创建一个视图:

CREATE VIEW all AS
SELECT m.id, m.title, m.description, m.date, m.views, r.rating, r.id
FROM riv_montage m, riv_ratings r

但我想在每一行上创建一个计算字段,其行为如下:

r.rating/COUNT(r.id)

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

我明白了,谢谢。

SELECT m.id, m.title, m.description, m.date, m.views,
(SELECT SUM(rating) FROM riv_ratings WHERE id = m.id) / (SELECT COUNT(*) FROM riv_ratings WHERE id = m.id) AS calc
FROM riv_montage m

性能方面很糟糕,但它完成了工作。

答案 1 :(得分:0)

不能说出它的表现:

CREATE VIEW all 
AS 
SELECT 
    r.rating, r.id, r.rating / (SELECT COUNT(id) FROM riv_ratings) 
FROM  
    riv_ratings r