Mysql,子查询问题

发布于 2024-10-16 19:29:06 字数 567 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ペ泪落弦音 2024-10-23 19:29:07

将“as SomeAlias”放在子查询之后:

Select rating_id, average_rating

From (Select rating_id, avg(rating_num) as average_rating
    from ratings
    group by rating_id
    having count(*) > 50) as A

HAVING average_rating > 4 ;

Put "as SomeAlias" after the subquery:

Select rating_id, average_rating

From (Select rating_id, avg(rating_num) as average_rating
    from ratings
    group by rating_id
    having count(*) > 50) as A

HAVING average_rating > 4 ;
梅倚清风 2024-10-23 19:29:06

正如错误消息所述,您需要为子查询添加别名:

SELECT rating_id, average_rating
FROM (
    SELECT
        rating_id,
        AVG(rating_num) AS average_rating
    FROM ratings
    GROUP BY rating_id
    HAVING COUNT(*) > 50
) AS some_alias
WHERE average_rating > 4 

some_alias 可以是任何内容 - 可以是子查询的描述性名称,也可以是因为您永远不需要通过名称引用子查询可以只使用非描述性名称,例如 T1(如果有其他子查询,则使用 T2、T3 等)。

您还可以在外部查询中使用 WHERE 而不是 HAVING。

As the error message states you need to add an alias for your subquery:

SELECT rating_id, average_rating
FROM (
    SELECT
        rating_id,
        AVG(rating_num) AS average_rating
    FROM ratings
    GROUP BY rating_id
    HAVING COUNT(*) > 50
) AS some_alias
WHERE average_rating > 4 

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.

橘和柠 2024-10-23 19:29:06
Select rating_id, average_rating

From (Select rating_id, avg(rating_num) as average_rating
    from ratings
    group by rating_id
    having count(*) > 50) a

HAVING average_rating > 4 ;

注意表别名“a”

Select rating_id, average_rating

From (Select rating_id, avg(rating_num) as average_rating
    from ratings
    group by rating_id
    having count(*) > 50) a

HAVING average_rating > 4 ;

Note the table alias "a"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文