22,209
社区成员
发帖
与我相关
我的任务
分享
create table province
(
id int,
name nvarchar(50)
)
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'
create table student
(
id int,
name nvarchar(50),
provinceId int
)
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2
create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3
create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200
--期望得到
--id name student teacher totalScore
--1 北京 2 1 130
--2 上海 3 1 200
--3 广东 0 2 0
create table province
(
id int,
name nvarchar(50)
)
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'
create table student
(
id int,
name nvarchar(50),
provinceId int
)
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2
create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3
create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200
--期望得到
--id name student teacher totalScore
--1 北京 2 1 130
--2 上海 3 1 200
--3 广东 0 2 0
select a.id,a.name,count(distinct b.name) as student,
count(distinct c.name) as teacher,isnull(sum(score),0) as totalScore
from province a
left join student b on a.id=b.provinceid
left join teacher c on a.id=c.provinceid
left join score d on b.id=d.studentid
group by a.id,a.name
order by a.id
id name student teacher totalScore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0
警告: 聚合或其他 SET 操作消除了 Null 值。
(3 行受影响)
---测试数据 创建表province
create table province
(
id int,
name nvarchar(50)
)
---向province插入记录
insert into province
select 1, '北京'
union all
select 2, '上海'
union all
select 3, '广东'
----创建表student
create table student
(
id int,
name nvarchar(50),
provinceId int
)
---student表插入记录
insert into student
select 1, '张三', 1
union all
select 2, '李四', 1
union all
select 3, '王二', 2
union all
select 4, '麻子', 2
union all
select 5, '赵五', 2
create table teacher
(
id int,
name nvarchar(50),
provinceId int
)
insert into teacher
select 1, '语文老师', 1
union all
select 2, '数学老师', 2
union all
select 3, '英语老师', 3
union all
select 4, '地理 老师', 3
create table score
(
studentId int,
score int
)
insert into score
select 1, 80
union all
select 2, 50
union all
select 3, 200
----查询
SQL codeselect a.id,a.name,count(distinct b.name) as student,
count(distinct c.name) as teacher,isnull(sum(score),0) as totalScore
from province a
left join student b on a.id=b.provinceid
left join teacher c on a.id=c.provinceid
left join score d on b.id=d.studentid
group by a.id,a.name
order by a.id
/*
id name student teacher totalScore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0
(3 行受影响)
*/
select a.id,a.name,count(distinct b.name) as student,
count(distinct c.name) as teacher,isnull(sum(score),0) as totalScore
from province a
left join student b on a.id=b.provinceid
left join teacher c on a.id=c.provinceid
left join score d on b.id=d.studentid
group by a.id,a.name
order by a.id
/**
id name student teacher totalScore
----------- -------------------------------------------------- ----------- ----------- -----------
1 北京 2 1 130
2 上海 3 1 200
3 广东 0 2 0
(3 行受影响)
**/