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

 

  

你可能感兴趣的:(left join)