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

     

      

  • 相关阅读:
    mysql优化——show processlist命令详解
    Nginx常见错误
    nginx应用总结(1)--基础认识和应用配置
    mac键盘在ubuntu下开启fn功能按键
    字符串逆序输出--递归
    getchar返回int类型
    c语言中字符串跨行书写的问题
    ascii
    c语言中不允许在函数外部给全局变量赋值
    CF和OF的区别
  • 原文地址:https://www.cnblogs.com/qiqiweige/p/4933338.html
Copyright © 2011-2022 走看看