Can you divide one alias by another in MySQL?

后端 未结 2 1864
没有蜡笔的小新
没有蜡笔的小新 2021-02-08 21:25

I have a multi-table query, similar to this (simplified version)

SELECT columns, count(table2.rev_id) As rev_count, sum(table2.rev_rating) As sum_rev_rating 
FRO         


        
2条回答
  •  没有蜡笔的小新
    2021-02-08 21:29

    You're not able to do calculations with aliases. One way of doing this would be to simply create another alias and order by that.

    SELECT columns, count(table2.rev_id) As rev_count, sum(table2.rev_rating) As sum_rev_rating, sum(table2.rev_rating)/count(table2.rev_id) as avg_rev_rating
    FROM table1
    LEFT JOIN table2
    ON table1.dom_id = table2.rev_domain_from 
    WHERE dom_lastreview != 0 AND rev_status = 1 
    GROUP BY dom_url 
    ORDER BY avg_rev_rating DESC
    

提交回复
热议问题