Mysql-CONCAT和GROUP_CONCAT组合使用场景

2021/12/23 19:07:29

本文主要是介绍Mysql-CONCAT和GROUP_CONCAT组合使用场景,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

拼接一个头明细信息

甚至可以直接把结果,转为实体或者xml或者json进行传输。
字符串的操作

SELECT
	XBLNR,
	BUDAT,
	BLDAT,
	ZYKNR,
	UMWRK,
	UMLGO,
	WERKS,
	LGORT,
	CONCAT(
		'[',
		GROUP_CONCAT(
			CONCAT(
				'{"MATNR":"',
				IFNULL(MATNR, ''),
				'","ZYKNN":"',
				ZYKNN,
				'","ERFME":"',
				ERFME,
				'","ERFMG":',
				IFNULL(ERFMG, 0),
				'}'
			) SEPARATOR ','
		),
		']'
	) ITEM
FROM
	(
		SELECT
			pod.purchaseOrderHeaderCode XBLNR,
			DATE_FORMAT(NOW(), '%Y%m%d') BUDAT,
			DATE_FORMAT(NOW(), '%Y%m%d') BLDAT,
			pod.sapPurchaseOrderCode ZYKNR,
			pod.sapFactory UMWRK,
			pod.sapWarehouseCode UMLGO,
			poh.sapOutFactoryCode WERKS,
			poh.sapOutWarehouseCode LGORT,
			pod.skuCode MATNR,
			pod.sapPurchaseOrderLineCode ZYKNN,
			IFNULL(pod.sapUnit, sku.orderUnit) ERFME,
			CASE IFNULL(pod.sapUnit, sku.orderUnit)
		WHEN sku.orderUnit THEN
			pod.receiptQty
		ELSE
			pod.receiptQty * IFNULL(sku.basicUnitNumerator, 1) / IFNULL(sku.basicUnitDenominator, 1)
		END ERFMG
		FROM
			purchase_order_detail pod
		INNER JOIN purchase_order_header poh ON poh.id = pod.purchaseOrderHeaderId
		INNER JOIN SKU ON sku.`code` = pod.skuCode
		AND poh.companyCode = sku.companyCode
		AND pod.receiptQty > 0
		WHERE
			pod.purchaseOrderHeaderId =:id
	) A
GROUP BY
	XBLNR,
	ZYKNR,
	UMWRK,
	UMLGO


这篇关于Mysql-CONCAT和GROUP_CONCAT组合使用场景的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程