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

     

      

  • 相关阅读:
    网页简单模块布局
    Navicat 8 注册密码
    布局黄冈中学
    php导出csv格式文件
    518. 零钱兑换 II
    1813. 句子相似性 III
    ransac算法概述
    c++ 读取文件夹下所有的文件名
    c++ 获取系统时间 写txt string 转 char* 文件改名 文件删除
    1498. 满足条件的子序列数目 二分 快速幂 等比数列前n项和公式
  • 原文地址:https://www.cnblogs.com/qiqiweige/p/4933338.html
Copyright © 2011-2022 走看看