我是SQL新手,正在尝试创建基本的子查询。我需要找到Manager中的平均年龄和Trainee中的平均年龄之间的差异。
标题是一栏-经理或实习生
年龄是一列,所有列都在同一表中。
我会使用两个子查询来执行以下操作吗:
Select manager_age - trainee_age
from book1
(select avg(age) as manager_age from book1 where title = "manager")
和另一个子查询:
(select avg(age) as manager_age from book1 where title = "trainee")
我不确定如何在一个查询中执行两个子查询,或者是否应该像联接一样以另一种方式进行?谢谢!
最佳答案
create table book1(title varchar(50), age integer) ;
insert into book1 values('Trainee', 10);
insert into book1 values('Trainee', 20);
insert into book1 values('Manager', 30);
insert into book1 values('Manager', 40);
insert into book1 values('Manager', 50);
select * from book1;
select m.m_age - t.t_age from (
(select title as m_title, avg(age) as m_age
from book1 where title = 'Manager') m,
(select title as t_title, avg(age) as t_age
from book1 where title = 'Trainee') t);
关于mysql - MySQL:子查询(基本),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55272443/