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
總結
- 上一篇: C&C++图形图像处理开源库
- 下一篇: SIM800C 使用基站定位