[英]mysql group_concat for left and right table
我正在使用group_concat
函數來聚合產品的一些移動,這些移動已經為帶有left join
右表(stockout)
完成了。 MySQL代碼如下-
SELECT
p.serialno AS SISN,
p.in_quantity AS INQTY,
SUM(IFNULL(s.out_quantity, '0')) AS OUTQTY,
GROUP_CONCAT(
CONCAT(
COALESCE(IFNULL(s.out_quantity,'0'), ''),'|'
)
) details
FROM stockin p LEFT JOIN stockout s
ON p.serialno = s.serialno
WHERE p.productid = 'TF00123'
GROUP BY p.stockin_id, p.serialno, s.serialno
輸出如下
+------+-------+--------+--------------------------------+
| SISN | INQTY | OUTQTY | details |
+------+-------+--------+--------------------------------+
| AAA1 | 500 | 740 | 300|,100|,50|,50|,20|,20|,200| |
| AAA2 | 500 | 0 | 0| |
| AAA3 | 1 | 3 | 1|,1|,1| |
| AAA3 | 1 | 3 | 1|,1|,1| |
| AAA1 | 200 | 740 | 300|,100|,50|,50|,20|,20|,200| |
| AAA3 | 1 | 3 | 1|,1|,1| |
| AAA1 | 100 | 740 | 300|,100|,50|,50|,20|,20|,200| |
+------+-------+--------+--------------------------------+
7 rows in set (0.00 sec)
現在,如果p.serialno是相同的,我想將左表數量的group_concat
放在另一列中,例如詳細信息。 如果我手動執行,請檢查輸出-
+------+-------+------------------+--------+--------------------------------+
| SISN | INQTY | details | OUTQTY | details |
+------+-------+------------------+--------+--------------------------------+
| AAA1 | 800 | 500|,200|,100| | 740 | 300|,100|,50|,50|,20|,20|,200| |
| AAA2 | 500 | 0| | 0 | 0| |
| AAA3 | 3 | 1|,1|,1| | 3 | 1|,1|,1| |
+------+-------+------------------+--------+--------------------------------+
3 rows in set (0.00 sec)
只需使用
GROUP_CONCAT(
CONCAT(
COALESCE(IFNULL(p.out_quantity,'0'), ''),'|'
)
) details1
像這樣
SELECT
p.serialno AS SISN,
p.in_quantity AS INQTY,
GROUP_CONCAT(
CONCAT(
COALESCE(IFNULL(p.out_quantity,'0'), ''),'|'
)
) details1
SUM(IFNULL(s.out_quantity, '0')) AS OUTQTY,
GROUP_CONCAT(
CONCAT(
COALESCE(IFNULL(s.out_quantity,'0'), ''),'|'
)
) details
FROM stockin p LEFT JOIN stockout s
ON p.serialno = s.serialno
WHERE p.productid = 'TF00123'
GROUP BY p.stockin_id, p.serialno, s.serialno
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.