主要是用
left join on 语句, 左连接 ... 列合并 ..... 以第1张表为主, 将第2张表的满足ON 条件的列加进来!!!!
语法:
select * from
A
left join
B
on A.a=B.a
example:
SELECT a.类别, a.名称, a.品牌, a.型号, a.进货数量, b.销售数量, iif( IsNull(a.进货数量),0, a.进货数量)-iif( IsNull(b.销售数量),0, b.销售数量 ) AS 库存量
FROM
[SELECT 类别, 名称, 品牌, 型号, sum(数量) AS 进货数量, sum(金额) AS 进货金额 FROM B_JHJL GROUP BY 类别, 名称, 品牌, 型号 ]. AS A
LEFT JOIN
[SELECT 类别, 名称, 品牌, 型号, sum(数量) AS 销售数量, sum(金额) AS 销售金额 FROM B_XSJL GROUP BY 类别, 名称, 品牌, 型号 ]. AS B
ON
(iif( IsNull(a.型号), '', a.型号 )=iif( IsNull(b.型号), '', b.型号 )) AND (iif( IsNull(a.品牌), '', a.品牌 )=iif( IsNull(b.品牌), '', b.品牌 )) AND (A.名称=B.名称) AND (A.类别=B.类别)