mysql组\u concat用于左表和右表
我正在使用mysql组\u concat用于左表和右表,mysql,sql,select,join,left-join,Mysql,Sql,Select,Join,Left Join,我正在使用group\u concat函数来聚合产品的一些移动,这些移动已经在右表(缺货)中使用左连接。下面是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.ou
group\u concat
函数来聚合产品的一些移动,这些移动已经在右表(缺货)
中使用左连接
。下面是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相同,我想将左表数量的分组到另一列中,如details。
如果我手动执行,请检查输出-
+------+-------+------------------+--------+--------------------------------+
| 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