首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL多个聚合函数和外连接

问MySQL多个聚合函数和外连接
EN

Stack Overflow用户
提问于 2017-11-30 11:36:12
回答 0查看 32关注 0票数 0

我有两个问题:

首先:

代码语言:javascript
复制
select x.year, sum(x.cnt)
from (select count(id)*3 as cnt from takes where year = 2009 group by year) x
group by x.year;

我得到的错误是“字段列表中的未知列x.year。我希望查询充当select year, sum(count(id)*3);

第二:

我需要编写一个查询,该查询在不使用外部连接的情况下执行与以下相同的操作:

代码语言:javascript
复制
select instructor.id, name, count(teaches.id) as 'Number of Sections'
from instructor natural left outer join teaches
group by teaches.id, name, instructor.id
order by instructor.id asc;

我尝试了以下方法,但不起作用:

代码语言:javascript
复制
(select instructor.id, name, count(teaches.id) as 'Number of Sections'
from instructor, teaches
where instructor.id = teaches.id
group by teaches.id, name, instructor.id)
union
(select i.id, i.name, count(t.id)
from instructor i, teaches t
where i.id not in (select id from teaches)
group by t.id, i.name, i.id);

谢谢!

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47565478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档