SQL - 缺少FROM子句

时间:2015-04-13 17:20:43

标签: sql postgresql

如何在使用嵌套的select语句时在select语句中添加其他列

SELECT 
     MAX (mycount), people.name
FROM 
    (SELECT 
         people.name a, COUNT(course_staff.staff) mycount
     FROM 
         course_staff, people
     WHERE 
         course_staff.staff = people.id
     GROUP BY 
         course_staff.staff,people.name) AS foo;

ERROR

  

缺少表“人”的FROM子句条目
  第1行:SELECT MAX(mycount),people.name

我想添加people.name

1 个答案:

答案 0 :(得分:2)

当您使用subquery from子句时,请尝试对其进行别名并使用alias prefix作为select子句,同样您必须将子查询的别名用于外部查询中的select语句:

SELECT MAX (foo.mycount), foo.a
FROM (SELECT people.name a, -- use "a" also for outer query for column name
             COUNT(course_staff.staff) mycount -- use "mycount" in outer query
      FROM course_staff, people
      WHERE course_staff.staff = people.id
      GROUP BY people.name
     ) AS foo
GROUP BY foo.a