You cannot use the alias '' of an expression containing a window function in this context.
侧边栏壁纸
  • 累计撰写 188 篇文章
  • 累计收到 24 条评论

You cannot use the alias '' of an expression containing a window function in this context.

五好
2021-11-24 / 0 评论 / 465 阅读 / 正在检测是否收录...

抛出这个错误一般是你使用窗口函数后直接在where子句里引用了窗口函数的结果,但是窗口函数的计算是在where子句之后才执行的,所以sql会抛出异常 ,那么该怎么解决这个问题呢?

1.使用子查询
2.使用 with的方法

如:
子查询的方式

select title,(select name from clas where id=top.cid)cname,view from (SELECT title,cid,view,row_number () over ( PARTITION BY `cid` ORDER BY view desc ) t  from book)top where t=1

with的方式

with top as(
SELECT title,cid,view,row_number () over ( PARTITION BY `cid` ORDER BY view desc ) t  from book)
select title,(select name from clas where id=top.cid)cname,view from top where t=1;

上述语句是从数据库中查询每个分类中点击最多的书,由此可以优雅的看出各个分类中那一本书最受欢迎。
而如果我们使用group by 来做类似的求值 一般会出现only_full_group_by的错误提示,而且也不好处理

0

评论 (0)

取消