Unknown column while using alias mysql

梦想的初衷 提交于 2020-01-14 06:17:10

问题


I am following SQL course from lagunita.satnford.edu. I am doing an exercise on practising queries, i have three tables:

Movie ( mID, title, year, director )

Reviewer ( rID, name )

Rating ( rID, mID, stars, ratingDate )

Problem Statement: Find the difference between the average rating of movies released before 1980 and the average rating of movies released after 1980. (Make sure to calculate the average rating for each movie, then the average of those averages for movies before 1980 and movies after. Don't just calculate the overall average rating before and after 1980.) I wrote the following query:

select max(a1) - min(a1) from
(
    select avg(av1) from
        (
            select avg(stars) av1
            from rating join movie m using(mID)
            where year < 1980
            group by mID
        ) as av1
    union
    select avg(av2) from
        (
            select avg(stars) av2
            from rating join movie m using(mID)
            where year > 1980
            group by mID
        ) as av2
) as a1;

I am getting the following error ERROR 1054 (42S22): Unknown column 'a1' in 'field list'

Commands to creare sample data:

/* Delete the tables if they already exist */
drop table if exists Movie;
drop table if exists Reviewer;
drop table if exists Rating;

/* Create the schema for our tables */
create table Movie(mID int, title text, year int, director text);
create table Reviewer(rID int, name text);
create table Rating(rID int, mID int, stars int, ratingDate date);

/* Populate the tables with our data */
insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming');
insert into Movie values(102, 'Star Wars', 1977, 'George Lucas');
insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise');
insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg');
insert into Movie values(105, 'Titanic', 1997, 'James Cameron');
insert into Movie values(106, 'Snow White', 1937, null);
insert into Movie values(107, 'Avatar', 2009, 'James Cameron');
insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg');

insert into Reviewer values(201, 'Sarah Martinez');
insert into Reviewer values(202, 'Daniel Lewis');
insert into Reviewer values(203, 'Brittany Harris');
insert into Reviewer values(204, 'Mike Anderson');
insert into Reviewer values(205, 'Chris Jackson');
insert into Reviewer values(206, 'Elizabeth Thomas');
insert into Reviewer values(207, 'James Cameron');
insert into Reviewer values(208, 'Ashley White');

insert into Rating values(201, 101, 2, '2011-01-22');
insert into Rating values(201, 101, 4, '2011-01-27');
insert into Rating values(202, 106, 4, null);
insert into Rating values(203, 103, 2, '2011-01-20');
insert into Rating values(203, 108, 4, '2011-01-12');
insert into Rating values(203, 108, 2, '2011-01-30');
insert into Rating values(204, 101, 3, '2011-01-09');
insert into Rating values(205, 103, 3, '2011-01-27');
insert into Rating values(205, 104, 2, '2011-01-22');
insert into Rating values(205, 108, 4, null);
insert into Rating values(206, 107, 3, '2011-01-15');
insert into Rating values(206, 106, 5, '2011-01-19');
insert into Rating values(207, 107, 5, '2011-01-20');
insert into Rating values(208, 104, 3, '2011-01-02');

回答1:


Please post question with sample data, which makes easy so test and proper answer.

In your code a1 is the name of the derived table not the column name.

Aggregate functions accept the parameter in terms of column name.

Try the following:

select max(av) - min(av) from
(
    select avg(av1) av from
        (
            select avg(stars) av1
            from rating join movie m using(mID)
            where year < 1980
            group by mID
        ) as av1
    union
    select avg(av2) av from
        (
            select avg(stars) av2
            from rating join movie m using(mID)
            where year > 1980
            group by mID
        ) as av2
) as a1; 


来源:https://stackoverflow.com/questions/48881904/unknown-column-while-using-alias-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!