mysql merges the two statistical result sets into one line

In the inventory management system, the quantity and amount of the surplus, sales, total (remaining + sales) of commodities should be counted. In order to reduce the operation of the database, use a SQL to find the results.

1 Remaining quantity and amount

 2 Sales volume and amount

Require the two-row result set to be merged into a row result set

The SQL operation is as follows:

EXPLAIN
select remain_total,remain_total_money,seller_count,seller_count_money,remain_total+seller_count as mileage_total,remain_total_money+seller_count_money as mileage_total_money  from (
SELECT
	IFNULL( sum( remain ), 0 ) remain_total,
	IFNULL( sum( remain * union_price ), 0 ) remain_total_money,
  1  'id'
FROM
	(
	SELECT
		wms_product_detail.* 
	FROM
		`wms_product_detail`,
		wms_product_detail_attribute 
	WHERE
		wms_product_detail.id = wms_product_detail_attribute.detail_id 
		AND wms_product_detail.`status` = 0 
		AND wms_product_detail.del_flag = 0 
	GROUP BY
	wms_product_detail.id 
	) AS product ) as tb1
	left join ( select * from (
	SELECT
		IFNULL( sum( count ), 0 ) seller_count,IFNULL(sum(now_price),0)  seller_count_money,1  'id'
	FROM
		( (
		SELECT
			wms_order.* 
		FROM
			`wms_product_detail`,
			wms_order,
			wms_product_detail_attribute 
		WHERE
			wms_product_detail.id = wms_order.detail_id 
			AND wms_product_detail.id = wms_product_detail_attribute.detail_id 
			AND wms_order.`status` = 0 
			AND wms_order.del_flag = 0 
		GROUP BY
			wms_order.id 
		) AS tmp ) ) as orderTable) as tb2 on tb1.id=tb2.id

 

Guess you like

Origin blog.csdn.net/Lixuanshengchao/article/details/103506449