字段值用逗号分隔时如何显示多行?

时间:2019-06-27 06:20:45

标签: mysql perl

我有两个表叫ctgr [ctgr_id是主键]和项[item_id是主键]。

ctgr表的值为:


  ctgr_id ||  ctgr_name
  =====================
     1    ||   laptop
     2    ||    pc

项目表的值为:


    item_id || ctgr_id || summary_ctgr_id || item_name  || item_price || stock
    =========================================================================
      1101  ||  1      ||      1          ||  laptop1   ||   100      || 100
      1102  ||  1      ||      1          ||  setup1    ||   0        || 0
      1103  ||  1      ||      1          ||  setup2    ||   0        || 0
      2101  ||  2      ||      2          ||  pc1       ||   200      || 50
      2102  ||  2      ||      2          ||  pc2       ||   300      || 50
      2103  ||  2      ||      2          ||  note-pc   ||   500      || 30
      2104  ||  2      ||      2          ||  pc3       ||   400      || 50

如果我按笔记本电脑类别选择,则查询如下所示:     

    item_id|ctgr_id|summary_ctgr_id|item_name   |item_price|stock|ctgr_name|total 
    =============================================================================
      1101 |1      |      1        |  laptop1   |   100    | 100 |laptop  |200
      1102 |1      |      1        |  setup1    |   0      | 0   |laptop  |0
      1103 |1      |      1        |  setup2    |   0      | 0   |laptop  |0
    

如果按PC类别选择,则输出如下所示:

    item_id|ctgr_id|summary_ctgr_id|item_name   |item_price|stock|ctgr_name|total 
    =============================================================================
      2101 |2      |      2        |  pc1       |   200    | 50  |pc      |0
      2102 |2      |      2        |  pc2       |   300    | 50  |pc      |200
      2103 |2      |      2        |  note-pc   |   500    | 30  |pc      |1000
      2104 |2      |      2        |  pc3       |   400    | 50  |pc      |400
 

我的查询在下面:

 SELECT
        item.*,
        ctgr.ctgr_name,
        v_sum_ord.item_total_price
        FROM
        item
        LEFT JOIN (
            SELECT
            item_id,
            sum(quantity) AS quantity,
            sum(item_price * quantity)  AS item_total_price
            FROM ord_item
            WHERE ord_item_status != 4
            GROUP BY item_id
        ) v_sum_ord ON item.item_id = v_sum_ord.item_id
        LEFT JOIN (
            SELECT
            item_set.child_id AS item_id,
            sum(quantity) AS quantity,
            sum(item_total_price)  AS item_total_price
            FROM (
                SELECT
                item_id,
                sum(quantity) AS quantity,
                sum(item_price * quantity)  AS item_total_price
                FROM ord_item
                WHERE ord_item_status != 4
                GROUP BY item_id
            ) v_sum_ord
            INNER JOIN item_set ON v_sum_ord.item_id = item_set.item_id
            GROUP BY 1
        ) v_sum_set_ord ON item.item_id = v_sum_set_ord.item_id
        LEFT JOIN ctgr ON item.summary_ctgr_id = ctgr.ctgr_id

但是我想要如下输出:

如果我按笔记本电脑类别选择,则需要如下输出:     

    item_id|ctgr_id|summary_ctgr_id|item_name   |item_price|stock|ctgr_name|total 
    =============================================================================
      1101 |1      |      1        |  laptop1   |   100    | 100 |laptop  |200
      1102 |1      |      1        |  setup1    |   0      | 0   |laptop  |0
      1103 |1      |      1        |  setup2    |   0      | 0   |laptop  |0
      2103 |2      |      2        |  note-pc   |   500    | 30  |pc      |500
    

如果我按PC类别选择,则需要如下输出:

    item_id|ctgr_id|summary_ctgr_id|item_name   |item_price|stock|ctgr_name|total 
    =============================================================================
      1101 |1      |      1        |  laptop1   |   100    | 100 |laptop  |200
      2101 |2      |      2        |  pc1       |   200    | 50  |pc      |0
      2102 |2      |      2        |  pc2       |   300    | 50  |pc      |200
      2103 |2      |      2        |  note-pc   |   500    | 30  |pc      |1000
      2104 |2      |      2        |  pc3       |   400    | 50  |pc      |400
 

0 个答案:

没有答案