错误代码:1054“字段列表”中的未知列“likes”

7d7tgy0s  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(423)

我的要求是得到平均的喜欢。

SELECT 
    COALESCE(SUM(type = 1), 0) AS likes,
    COALESCE(SUM(type = 2), 0) AS dislikes,
    count(type) as total_like,
    likes / dislikes, // Error on this line
    e_id  
FROM `e_rating` 
group by e_id 
order by e_id ASC

注意:仅使用单个查询获取平均值。

ukqbszuj

ukqbszuj1#

您不能引用以前的别名,因此需要写出完整的语句:

SELECT 
    COALESCE(SUM(type = 1), 0) AS likes,
    COALESCE(SUM(type = 2), 0) AS dislikes,
    count(type) as total_like,
    COALESCE(SUM(type = 1), 0) / COALESCE(SUM(type = 1), 0) AS average,
    e_id  
FROM `e_rating` 
group by e_id 
order by e_id ASC

相关问题