日常涨知识系列-惊天地泣鬼神的 sql, 300 行~~

2020-12-23 14:19:12 +08:00
 funbox

没有别的意思就是想让大家,长长见识~ 能看懂算我输

SELECT
	* 
FROM
	(
	SELECT
		`inventory`.`id` AS `inventoryId`,
		`inventory`.`location_id` AS `locationId`,
		`inventory`.`storehouse_id` AS `storehouseId`,
		`product`.`id` AS `productId`,
		`product`.`name` AS `name`,
		`product`.`number` AS `number`,
		`brand`.`name` AS `brandName`,
		`product`.`size` AS `size`,
		`category`.`name` AS `categoryName`,
		`product`.`unit` AS `unit`,
		`storehouse`.`name` AS `storehouseName`,
		`location`.`name` AS `locationName`,
		IFNULL( inventory.quantity, 0 ) AS `remainQuantity`,
		IFNULL( inventoryBath.avgPrice, 0 ) AS `avgPrice`,
		IFNULL( inventoryBath.totalPrice, 0 ) AS `totalPrice`,
		`inventoryWarning`.`lowerLimit` AS `lowerLimit`,
		`inventoryWarning`.`upperLimit` AS `upperLimit`,(
			IFNULL( repairOrderItem.repairRemainQuantity, 0 )+ IFNULL( purchaseReturnOrderItem.purchaseReturnRemainQuantity, 0 )+ IFNULL( otherOutOrderItem.otherInOutstockQuantity, 0 )) AS `waitOutQuantity`,(
		IFNULL( purchaseOrderItem.purchaseRemainQuantity, 0 )+ IFNULL( otherInOrderItem.otherInOutstockQuantity, 0 )+ IFNULL( onlineInOrderItem.onlineRemainInstockQuantity, 0 )) AS `waitIntQuantity`,
		`product`.`barcode` AS `barcode`,
		`supplier`.`name` AS `supplierName`,
		`product`.`valid_period` AS `validPeriod`,
		`product`.`standard_id` AS `standardId`,
		`product`.`from_id` AS `fromId`,
		`product`.`source` AS `source`,
		`oeTable`.`oeNumber` AS `oeNumber`,
		`product`.`vehicle_note` AS `vehicleNote`,
		`product`.`description` AS `note`,
		`store`.`name` AS `storeName`,
		`product`.`create_time` AS `createTime`,
		`inventory`.`tenant_id`,
		`product`.`sale_price1` AS `productSalePrice`,
		`product`.`price_mode` AS `productPriceMode`,
		`product`.`markup_type` AS `productMarkupType`,
		IFNULL( lastPurchasePrice.lastPurchasePrice, 0 ) AS `lastPurchasePrice`,(
		CASE
				
				WHEN product.price_mode = 1 THEN
				product.sale_price1 
				WHEN product.price_mode = 2 
				AND product.markup_type = 1 
				AND IFNULL( inventory.quantity, 0 )= 0 THEN
					IFNULL( lastOutPrice.unitPrice, 0 )* product.sale_price1 
					WHEN product.price_mode = 2 
					AND product.markup_type = 1 
					AND IFNULL( inventory.quantity, 0 ) != 0 THEN
						IFNULL( inventoryBath.avgPrice, 0 )* product.sale_price1 
						WHEN product.price_mode = 2 
						AND product.markup_type != 1 THEN
							IFNULL( lastPurchasePrice.lastPurchasePrice, 0 )* product.sale_price1 
						END 
						) AS salePrice 
					FROM
						`inventory`
						LEFT JOIN `product` ON `inventory`.`product_id` = `product`.`id`
						LEFT JOIN `category` ON `product`.`category_id` = `category`.`id`
						LEFT JOIN `brand` ON `product`.`brand_id` = `brand`.`id`
						LEFT JOIN `storehouse` ON `inventory`.`storehouse_id` = `storehouse`.`id`
						LEFT JOIN `location` ON `inventory`.`location_id` = `location`.`id`
						LEFT JOIN `supplier` ON `product`.`supplier_id` = `supplier`.`id`
						LEFT JOIN `store` ON `inventory`.`store_id` = `store`.`id`
						LEFT JOIN (
						SELECT
							SUBSTRING_INDEX(
								GROUP_CONCAT( unit_price ORDER BY outstock_time DESC ),
								',',
								1 
							) AS `unitPrice`,
							`product_id` AS `productId` 
						FROM
							`inventory_batch` 
						WHERE
							( `store_id` = '27358' ) 
							AND ( `inventory_batch`.`tenant_id` = 1 ) 
						GROUP BY
							`product_id` 
						) `lastOutPrice` ON `inventory`.`product_id` = lastOutPrice.productId
						LEFT JOIN (
						SELECT
							group_concat( product_number.number SEPARATOR ";" ) AS `oeNumber`,
						`product_number`.`product_id` AS `productId` 
						FROM
							`product_number` 
						WHERE
							( `product_number`.`type` = 1 ) 
							AND ( `product_number`.`tenant_id` = 1 ) 
						GROUP BY
							`product_number`.`product_id` 
						) `oeTable` ON `inventory`.`product_id` = oeTable.productId
						LEFT JOIN (
						SELECT
							`storehouse_id` AS `storehouseId`,
							SUM( quantity )- SUM( outstock_quantity ) AS `remainQuantity`,
							SUM(
								unit_price *(
									quantity - outstock_quantity 
								)) AS `totalPrice`,
							`product_id` AS `productId`,
						IF
							(
								inventoryBathAll.currentQuantity = 0,
								inventoryBathLast.unitPrice,
							round( inventoryBathAll.avgPrice, 2 )) AS `avgPrice` 
						FROM
							`inventory_batch`
							LEFT JOIN (
							SELECT
								`product_id` AS `productId`,
								SUM( quantity )- SUM( outstock_quantity ) AS `currentQuantity`,
								CONVERT (
									SUM(
										unit_price *(
											quantity - outstock_quantity 
										))/(
									SUM( quantity )- SUM( outstock_quantity )),
								DECIMAL ( 20, 2 )) AS `avgPrice` 
							FROM
								`inventory_batch` 
							WHERE
								( `store_id` = '27358' ) 
								AND ( `inventory_batch`.`tenant_id` = 1 ) 
							GROUP BY
								`product_id` 
							) `inventoryBathAll` ON `inventory_batch`.`product_id` = inventoryBathAll.productId
							LEFT JOIN (
							SELECT
								SUBSTRING_INDEX(
									GROUP_CONCAT( unit_price ORDER BY batch_number DESC ),
									",",
									1 
								) AS `unitPrice`,
								`product_id` AS `productId`,
								max( batch_number ) AS `batchNumber` 
							FROM
								`inventory_batch` 
							WHERE
								( `store_id` = '27358' ) 
								AND ( `inventory_batch`.`tenant_id` = 1 ) 
							GROUP BY
								`product_id` 
							ORDER BY
								`batch_number` DESC 
							) `inventoryBathLast` ON `inventory_batch`.`product_id` = inventoryBathLast.productId 
						WHERE
							( `store_id` = '27358' ) 
							AND ( `inventory_batch`.`tenant_id` = 1 ) 
						GROUP BY
							`product_id`,
							`storehouse_id` 
						) `inventoryBath` ON ( `inventory`.`product_id` = inventoryBath.productId ) 
						AND ( `inventory`.`storehouse_id` = inventoryBath.storehouseId )
						LEFT JOIN (
						SELECT
							`product_id` AS `productId`,
							`lower_limit` AS `lowerLimit`,
							`upper_limit` AS `upperLimit` 
						FROM
							`inventory_warning` 
						WHERE
							( `store_id` = '27358' ) 
						AND ( `inventory_warning`.`tenant_id` = 1 )) `inventoryWarning` ON `inventory`.`product_id` = inventoryWarning.productId
						LEFT JOIN (
						SELECT
							SUM( repair_order_product_item.quantity - repair_order_product_item.use_quantity ) AS `repairRemainQuantity`,
							`repair_order_product_item`.`product_id` AS `productId` 
						FROM
							`repair_order_product_item`
							INNER JOIN (
							SELECT
								* 
							FROM
								`repair_order` 
							WHERE
								( `status` != 21000 ) 
								AND ( `repair_order`.`store_id` = '27358' ) 
							AND ( `repair_order`.`tenant_id` = 1 )) `repairOrder` ON `repair_order_product_item`.`order_id` = repairOrder.id
							INNER JOIN (
							SELECT DISTINCT
								`product_id` 
							FROM
								`inventory` 
							WHERE
								( `store_id` = '27358' ) 
								AND ( `inventory`.`tenant_id` = 1 ) 
							GROUP BY
								`product_id` 
							) `inventoryQue` ON `repair_order_product_item`.`product_id` = inventoryQue.product_id 
						WHERE
							`repair_order_product_item`.`tenant_id` = 1 
						GROUP BY
							`repair_order_product_item`.`product_id` 
						) `repairOrderItem` ON `inventory`.`product_id` = repairOrderItem.productId
						LEFT JOIN (
						SELECT
							SUM( purchase_return_order_item.quantity - purchase_return_order_item.outstock_quantity ) AS `purchaseReturnRemainQuantity`,
							`purchase_return_order_item`.`product_id` AS `productId` 
						FROM
							`purchase_return_order_item`
							INNER JOIN (
							SELECT
								* 
							FROM
								`purchase_return_order` 
							WHERE
								(
								`purchase_return_order`.`status` IN ( 12000, 13000 )) 
								AND ( `purchase_return_order`.`store_id` = '27358' ) 
							AND ( `purchase_return_order`.`tenant_id` = 1 )) `purchaseReturnOrder` ON `purchase_return_order_item`.`order_id` = purchaseReturnOrder.id 
						WHERE
							`purchase_return_order_item`.`tenant_id` = 1 
						GROUP BY
							`purchase_return_order_item`.`product_id` 
						) `purchaseReturnOrderItem` ON `inventory`.`product_id` = purchaseReturnOrderItem.productId
						LEFT JOIN (
						SELECT
							SUM( other_in_out_stock_order_item.quantity ) AS `otherInOutstockQuantity`,
							`other_in_out_stock_order_item`.`product_id` AS `productId` 
						FROM
							`other_in_out_stock_order_item`
							INNER JOIN (
							SELECT
								* 
							FROM
								`other_in_out_stock_order` 
							WHERE
								( `other_in_out_stock_order`.`type` = 1 ) 
								AND ( `other_in_out_stock_order`.`status` = 1 ) 
								AND ( `other_in_out_stock_order`.`store_id` = '27358' ) 
							AND ( `other_in_out_stock_order`.`tenant_id` = 1 )) `otherInOutOrder` ON `other_in_out_stock_order_item`.`order_id` = otherInOutOrder.id 
						WHERE
							`other_in_out_stock_order_item`.`tenant_id` = 1 
						GROUP BY
							`other_in_out_stock_order_item`.`product_id` 
						) `otherOutOrderItem` ON `inventory`.`product_id` = otherOutOrderItem.productId
						LEFT JOIN (
						SELECT
							SUM( purchase_order_item.quantity - purchase_order_item.instock_quantity ) AS `purchaseRemainQuantity`,
							`purchase_order_item`.`product_id` AS `productId` 
						FROM
							`purchase_order_item`
							INNER JOIN (
							SELECT
								* 
							FROM
								`purchase_order` 
							WHERE
								(
								`purchase_order`.`status` IN ( 12000, 13000 )) 
								AND ( `purchase_order`.`store_id` = '27358' ) 
							AND ( `purchase_order`.`tenant_id` = 1 )) `purchaseOrder` ON `purchase_order_item`.`order_id` = purchaseOrder.id 
						WHERE
							`purchase_order_item`.`tenant_id` = 1 
						GROUP BY
							`purchase_order_item`.`product_id` 
						) `purchaseOrderItem` ON `inventory`.`product_id` = purchaseOrderItem.productId
						LEFT JOIN (
						SELECT
							SUM( other_in_out_stock_order_item.quantity ) AS `otherInOutstockQuantity`,
							`other_in_out_stock_order_item`.`product_id` AS `productId` 
						FROM
							`other_in_out_stock_order_item`
							INNER JOIN (
							SELECT
								* 
							FROM
								`other_in_out_stock_order` 
							WHERE
								( `other_in_out_stock_order`.`type` = 2 ) 
								AND ( `other_in_out_stock_order`.`status` = 1 ) 
								AND ( `other_in_out_stock_order`.`store_id` = '27358' ) 
							AND ( `other_in_out_stock_order`.`tenant_id` = 1 )) `otherInOutOrder` ON `other_in_out_stock_order_item`.`order_id` = otherInOutOrder.id 
						WHERE
							`other_in_out_stock_order_item`.`tenant_id` = 1 
						GROUP BY
							`other_in_out_stock_order_item`.`product_id` 
						) `otherInOrderItem` ON `inventory`.`product_id` = otherInOrderItem.productId
						LEFT JOIN (
						SELECT
							SUM( online_purchase_order_item.quantity - online_purchase_order_item.instock_quantity ) AS `onlineRemainInstockQuantity`,
							`online_purchase_order_item`.`product_id` AS `productId` 
						FROM
							`online_purchase_order_item`
							INNER JOIN (
							SELECT
								* 
							FROM
								`online_purchase_order` 
							WHERE
								( `online_purchase_order`.`status` = 1 ) 
								AND ( `online_purchase_order`.`store_id` = '27358' ) 
							AND ( `online_purchase_order`.`tenant_id` = 1 )) `onlineOrder` ON `online_purchase_order_item`.`order_id` = onlineOrder.id 
						WHERE
							`online_purchase_order_item`.`tenant_id` = 1 
						GROUP BY
							`online_purchase_order_item`.`product_id` 
						) `onlineInOrderItem` ON `inventory`.`product_id` = onlineInOrderItem.productId
						LEFT JOIN (
						SELECT
							`product_id` AS `productId`,
							`last_price` AS `lastPurchasePrice` 
						FROM
							`purchase_price` 
						WHERE
							( `store_id` = '27358' ) 
						AND ( `purchase_price`.`tenant_id` = 1 )) `lastPurchasePrice` ON `inventory`.`product_id` = lastPurchasePrice.productId 
					WHERE
						( `product`.`status` != 90 ) 
						AND ( `inventory`.`store_id` = '27358' ) 
					AND ( `inventory`.`tenant_id` = 1 )) `table` 
				WHERE
					`table`.`tenant_id` = 1 
				ORDER BY
				`createTime` DESC 
	LIMIT 10
13426 次点击
所在节点    MySQL
109 条回复
jasonkayzk
2020-12-23 22:07:59 +08:00
想知道这种 SQL 的性能如何= =
Felldeadbird
2020-12-23 22:24:56 +08:00
这 SQL 写得很好啊。至少说明 DB 命名上 规范。如果楼主遇到过一推 AS a, b ,c, d...

我很好奇这代码性能。。。

最后,楼主直接贴代码,不怕问责吗?
AntoniotheFuture
2020-12-23 22:26:37 +08:00
小意思啦,我之前一个项目一条 SQL3000 行而且有 4 条,跑一个小时,后来我亚索成 1000 行了,运行时间降到 20 分钟
thetbw
2020-12-23 22:30:28 +08:00
还好,我们公司 4,5 百行的也有,还是一个 sql 联接 10 多张表的,加上子查询。只能说公司服务器真的强大,还有就是访问量也没有很大,接口速度还能接受
ZhaoHongXuan
2020-12-23 22:36:11 +08:00
odps 上跑的?
uselessVisitor
2020-12-23 22:55:27 +08:00
@roundgis 青铜器我记得就是这样的好像
kevinjaz
2020-12-23 23:22:08 +08:00
我觉得还好呀
fatestigma
2020-12-23 23:28:04 +08:00
让我想起了当年上数据库课,几个加分题都是这种上百行的,写完之后过一天就看不懂自己写的啥了
FieldFarmer
2020-12-23 23:43:39 +08:00
很多项目直接把业务逻辑写在 oracle 里面的…用存储过程
Tink
2020-12-24 00:56:28 +08:00
这不是手写的,这绝壁不是
loading
2020-12-24 01:17:31 +08:00
看着挺常规
AkideLiu
2020-12-24 01:28:10 +08:00
Oracle : 别难为 MySQL,有啥事冲我来
laicanwen
2020-12-24 08:09:40 +08:00
我在前公司做 ERP 的时候没有一个存储过程少于 300 行的,而且字段名还是无意义编号,看一个字段查一遍手册。那会要是每个同事写的存储过程能有这样我就谢天谢地了。
Lancerer
2020-12-24 08:32:42 +08:00
这玩意在 mybatis 里的 xml 看才是难受,先把他转成 sql 再说吧。。。
ming7435
2020-12-24 08:42:17 +08:00
我直接就好家伙,这哥们一辈子不会被辞退了,除非公司倒闭~
weizhen199
2020-12-24 08:48:04 +08:00
一万行的 SP 看过,一句写了三百行的还蛮勇的
polymerdg
2020-12-24 08:57:32 +08:00
才几百行,这里 1000 行以上的 多得很
cnzjl
2020-12-24 09:00:28 +08:00
挺正常的
murmur
2020-12-24 09:01:06 +08:00
直观感觉还行,格式良好,命名工整,就是看不懂高端 SQL
另外,我真听过 SQL 写业务的(存储过程),美其名曰直接热更不需要重启,我竟然无话可说
chengz
2020-12-24 09:07:18 +08:00
其实还好,缩进正常的还是很好看的
只是新的互联网应用这种查询可能比较少见

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/738226

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX