zoukankan      html  css  js  c++  java
  • left join

    SELECT
    	s.UNIT_UID AS supplierId,
    	s.UNIT_NAME as supplierName,
    	SUM(IFNULL(d.size, 0)) AS orderNum,
    	SUM(IFNULL(d.sum, 0)) AS orderPrice,
    	SUM(IFNULL(d.receive, 0)) AS receiveNum,
    	SUM(
    		IFNULL(d.receive, 0) * IFNULL(d.price, 0)
    	) AS receivePrice
    FROM bas_supplier s LEFT JOIN
    pchs_bill b ON s.unit_uid=b.supplier_uid
    LEFT JOIN pchs_detail d ON b.bill_uid = d.bill_uid
    WHERE
    	b.com_uid = 'F9737750024F3E079DF57F8B3D50C84E'
    AND b.bill_date >= '2014 - 09 - 20 00 : 00 : 00'
    AND b.bill_date <= '2015 - 11 - 02 23 : 59 : 59'
    GROUP BY
    	s.UNIT_UID
    order by orderNum DESC,orderPrice DESC

     pchs_bill表中的null记录不会被查出来,先连接后查询,where后面的条件会将null过滤掉

    SELECT s.unit_uid as supplierId,s.UNIT_NAME as supplierName,IFNULL(m.orderNum, 0) as orderNum,
    IFNULL(m.orderPrice, 0) as orderPrice,
    IFNULL(m.receiveNum, 0) as receiveNum ,IFNULL(m.receivePrice, 0) as receivePrice
     FROM bas_supplier s LEFT JOIN (
    SELECT
    	b.supplier_uid AS supplierId,
    	SUM(IFNULL(d.size, 0)) AS orderNum,
    	SUM(IFNULL(d.sum, 0)) AS orderPrice,
    	SUM(IFNULL(d.receive, 0)) AS receiveNum,
    	SUM(
    		IFNULL(d.receive, 0) * IFNULL(d.price, 0)
    	) AS receivePrice
    FROM 
    pchs_bill b 
    LEFT JOIN pchs_detail d ON b.bill_uid = d.bill_uid
    WHERE
    	b.com_uid = 'F9737750024F3E079DF57F8B3D50C84E'
    AND b.bill_date >= '2014 - 07 - 20 00 : 00 : 00'
    AND b.bill_date <= '2015 - 11 - 02 23 : 59 : 59'
    GROUP BY
    	b.supplier_uid
    ) m ON m.supplierId=s.unit_uid
    order by IF(ISNULL(orderNum),1,0),orderNum DESC,
    IF(ISNULL(orderPrice),1,0),orderPrice DESC

     pchs_bill表中的null记录会被查出来,先where查询后连接,不会过滤null

     

      

  • 相关阅读:
    Windows环境下OpenLDAP安装配置
    jobcenter在Windows下连携LDAP
    OpenLDAP搭建
    Go 函数 #3
    Go 数组/多维数组/切片/map #2
    Go内置类型/变量/常量 #1
    git常用命令
    makefile基础_1
    kubernete的service
    配置开发环境
  • 原文地址:https://www.cnblogs.com/qiqiweige/p/4933338.html
Copyright © 2011-2022 走看看