如何在使用嵌套的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
答案 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