我正在使用group_concat
函数来聚合已经为右表完成的产品的少量(stockout)
动作left join
。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)
group_concat
现在,如果 p.serialno 相同,我想在另一列中留下表格数量,例如详细信息。如果我手动执行,请检查输出 -
+------+-------+------------------+--------+--------------------------------+
| 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)