在SQL中使用大小写时出错

时间:2017-01-27 23:48:30

标签: sql

我正在跟随Ben O. Smith的数据科学家SQL简介,但我在本地mysql上的第9页上的代码遇到了问题:

PDF网址:http://downloads.bensresearch.com/SQL.pdf

SQL错误(1054):未知列' s.id'在' where子句'

我遵循了pg上列出的完全相同的数据结构。但是不能绕过错误。

以下代码是否有错误?

select 
    id, 
    name, 
    (select case
                when avg_gpa >= 3.5 then 2
                when avg_gpa < 3.5 and avg_gpa >= 3.0 then 1
                else 0
                end as gpa_type 
    from
        (select avg(gpa) as avg_gpa
         from term_gpa
         where id=s.id) as avg_gpa_table
    ) as gpa_type
from 
    student as s
ORDER BY 
    name ASC;

1 个答案:

答案 0 :(得分:0)

尝试像这样重写它......

select s.id
     , s.name
     , case
            when avg_gpa >= 3.5                   then 2
            when avg_gpa < 3.5 and avg_gpa >= 3.0 then 1
            else 0
        end as gpa_type 
from student as s
LEFT JOIN  (           
            select id , avg(gpa) as avg_gpa
            from term_gpa
            Group by id
           ) as avg_gpa_table
ON avg_gpa_table.id = s.id
ORDER BY s.name ASC;