Mysql,子查询问题
Select rating_id, average_rating
From (Select rating_id, avg(rating_num) as average_rating
from ratings
group by rating_id
having count(*) > 50)
HAVING average_rating > 4 ;
运行查询后,出现错误
每个派生表必须有自己的别名
我知道这里的部分有效:
Select rating_id, avg(rating_num) as average_rating
from ratings
group by rating_id
having count(*) > 50
我在这个子查询中做错了什么?我找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找啊找不到错误,不管哪里改正,还是有错误
Select rating_id, average_rating
From (Select rating_id, avg(rating_num) as average_rating
from ratings
group by rating_id
having count(*) > 50)
HAVING average_rating > 4 ;
After running the query, I get an error
Every derived table must have its own alias
I know that the section here works:
Select rating_id, avg(rating_num) as average_rating
from ratings
group by rating_id
having count(*) > 50
What am I doing wrong in this subquery? I searched and searched and searched but couldn't find the mistake, no matter where I corrected, I still get errors
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将“as SomeAlias”放在子查询之后:
Put "as SomeAlias" after the subquery:
正如错误消息所述,您需要为子查询添加别名:
some_alias
可以是任何内容 - 可以是子查询的描述性名称,也可以是因为您永远不需要通过名称引用子查询可以只使用非描述性名称,例如 T1(如果有其他子查询,则使用 T2、T3 等)。您还可以在外部查询中使用 WHERE 而不是 HAVING。
As the error message states you need to add an alias for your subquery:
The
some_alias
can be anything - either a descriptive name for the subquery, or else since you never need to refer to the subquery by name you can just use non-descriptive names such as T1 (then T2, T3 etc. if you have other subqueries).Also you can use WHERE in your outer query rather than HAVING.
注意表别名“a”
Note the table alias "a"