嵌套的Select语句查询,错误“关键字'GROUP'附近的语法不正确”

时间:2017-01-24 16:45:20

标签: mysql sql

我已经阅读了一些与我的错误有关的帖子,但是没有一个解决方案似乎有效。我对SQL很新,所以很抱歉,如果它真的很简单。我有两张桌子

  1. 电影广告资源 - 其中包含movie_title,onhand_qty和replacement_price

  2. NotFlix - 有subscriber_name,queue_nbr和movie_title

  3. 我正在尝试加入这两个表来输出每个客户的总重置价格成本,但是当我这样做时会给出上面标题的错误。这是我的代码,提前感谢任何帮助!

    SELECT subscriber_name, SUM (replacement_price) as replacement
    
    FROM
    
    (SELECT NotFlix.subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price
    
    FROM NotFlix 
    
    INNER JOIN MovieInventory
    
    ON NotFlix.movie_title = MovieInventory.movie_title
    )
    
    GROUP BY subscriber_name;
    

2 个答案:

答案 0 :(得分:1)

您缺少别名:

SELECT AliasNameHere.subscriber_name, SUM (AliasNameHere.replacement_price) as replacement

FROM

(SELECT NotFlix.subscriber_name as subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price as replacement_price

FROM NotFlix 

INNER JOIN MovieInventory

ON NotFlix.movie_title = MovieInventory.movie_title
) AliasNameHere

GROUP BY subscriber_name;

答案 1 :(得分:-1)

我只是不知道为什么你在FROM子句中做一个临时表,你可以在这里做一个基本的INNER JOIN,并且可以避免别名的问题:

SELECT NotFlix.subscriber_name, SUM (MovieInventory.replacement_price) as replacement

FROM NotFlix 

INNER JOIN MovieInventory

ON NotFlix.movie_title = MovieInventory.movie_title

GROUP BY subscriber_name;