sqlserver多表查询
Ⅰ sqlserver 同时查询多表数据
在sqlserver中新建个视图,很直观看,相查哪个数据,sql语句直接就出来了。
Ⅱ sqlserver多表联合查询
selectc.discount
froma,b,cwherea.hw_name=b.hw_name
andb.sort_id=c.sort_id
anda.hw_name='苹果'
Ⅲ sqlserver多表查询语句
select a.*,b.remark,c.content from a,b,c where patindex('%'+convert(varchar,b.id)+'%',a.fb)>0
or patindex('%'+convert(varchar,c.id)+'%',a.fb)>0
Ⅳ sqlserver多表查询
既然三个表都是有关联的,先单表统计
,再合起来统计。。
select
A.*,BC.sum_B,BC.sum_C
from
A表
as
A
join
(
select
单位编码,sum_B,sum_C
from
(select
单位编码,sum(数据B)
as
sum_B
from
B表
group
by
单位编码)
as
B
join
(select
单位编码,sum(数据C)
as
sum_C
from
C表
group
by
单位编码)
as
C
on
B.单位编码=C.单位编码
)
as
BC
on
A.单位编码=BC.单位编码
简化下:
select
A.*,B.sum_B,C.sum_C
from
A表
as
A
join
(select
单位编码,sum(数据B)
as
sum_B
from
B表
group
by
单位编码)
as
B
on
A.单位编码=B.单位编码
join
(select
单位编码,sum(数据C)
as
sum_C
from
C表
group
by
单位编码)
as
C
on
A.单位编码=C.单位编码
Ⅳ sqlserver多表联合查询
select a.a_name as 名字,count(b.a_id) as 数量 from a inner join b on a.a_id = b.a_id group by a.a_name
名字 数量
me 3
wo 1
he 1
select a.a_name as 名字,count(b.a_id) as 数量 from a left join b on a.a_id = b.a_id group by a.a_name
名字 数量
me 3
wo 1
he 1
she 0
our 0