左表和右表的mysql group_concat

[英]mysql group_concat for left and right table


I am using group_concat function to aggregate few movements of a product which already done for right table (stockout) with left join. MySQL code is below-

我正在使用group_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

Output as follows-

輸出如下 -

+------+-------+--------+--------------------------------+
| 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)

Now, I want to group_concat of left table quantities in another column like details if p.serialno is same. Please check the output if i do manually-

現在,如果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)

1 个解决方案

#1


0  

Simply use

 GROUP_CONCAT(
            CONCAT(
                COALESCE(IFNULL(p.out_quantity,'0'), ''),'|'
            )
        ) details1

Like this

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/06/26/889f5a89244a2861f32e698832df9d93.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com