sql中多表联合查询:
创建表:
A表字段和数据举例:
部门 客户编号 订单号
======================
部门1 1001 号码1
部门2 1001 号码2
部门3 1002 号码3
B表字段和数据举例:
部门 分公司
==============
部门1 分公司1
部门2 分公司1
部门4 分公司4
C表字段和数据举例:
分公司机构 客户编号 客户名称
================================
分公司1 1001 客户1
分公司1 1001 客户1
分公司2 1003 客户3
联合查询语句:
select a.部门,a.客户编号,c.客户名称,a.订单号,b.分公司机构 from
A as a left join B as b
on a.部门=b.部门
left join C as c
on a.客户编号=c.客户编号
SELECT A.MName AS 姓名,(SELECT AVG(C.SCORE) FROM SCORE AS C WHERE C.FID IN (SELECT F.FID FROM F WHERE F.FNAME='语文')) AS 语文,(SELECT AVG(C.SCORE) FROM SCORE AS C WHERE C.FID IN (SELECT F.FID FROM F WHERE F.FNAME='数学')) AS 数学,(SELECT AVG(C.SCORE) FROM SCORE AS C WHERE C.FID IN (SELECT F.FID FROM F WHERE F.FNAME='英语')) AS 英语,(SELECT AVG(C.SCORE) FROM SCORE AS C WHERE C.FID IN (SELECT F.FID FROM F WHERE F.FNAME='历史')) AS 历史 FROM MEMBER AS A