码迷,mamicode.com
首页 > 其他好文 > 详细

LEFT JOIN 多表查询的应用

时间:2016-01-13 15:56:13      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

表结构如下:只把主要字段列出

表一:付款记录表  Gather

        字段:GatherID ,

       AccountID,

                PayMents 金额,

                PayWay  付款方式 1 现金 2 刷卡 

 表2:销售记录表 Account

  字段:AccountID

     仓库:StorageID

     时间:BeginTime   销售时间

 表3:仓库表 Storage

  字段 StorageID

    StorageName

现要求查出某个仓库某给时间段 总营业收入,现金收入,刷卡收入

SQL语句如下:

SELECT S.StorageName,S.StoreageID SUM(G.PayMents) as Total ,SUM(T.PayMents) as Cash,SUM(H.PayMents) as Card

FROM Storage S,Account A, Gather G

LEFT JOIN Gather T ON T.GatherID=G.GatherID AND T.PayWay=‘1‘

LEFT JOIN Gather H ON H.GatherID=G.GatherID AND H.PayWay=‘2‘

WHERE  S.StorageID=A.StorageID AND A.AccountID=G.AccountID AND A.BeginTime>‘2016-01-01‘ AND A.BeginTime<‘2016-01-10‘ AND S.StorageID=1123

 

LEFT JOIN 多表查询的应用

标签:

原文地址:http://www.cnblogs.com/shenxiaojie/p/5127384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!