MySQL将两列合并为一列,另一行

时间:2018-11-24 16:39:07

标签: mysql

我有以下输出

+--------------+---------------+-----------+-----+--------------+
| officer_name | supplier_name | item_name | qty | order_status |
+--------------+---------------+-----------+-----+--------------+
| A            | S1            | B5        |  21 | purchase     |
| B            | S1            | B5        |  20 | purchase     |
| C            | S2            | B5        |  -2 | issue        |
| D            | S3            | B5        |  -1 | issue        |
| A            | S2            | B5        |  -1 | issue        |
| A            | S2            | B5        |  -1 | issue        |
| B            | S4            | B5        |  -1 | issue        |
| C            | S4            | B5        |  -1 | issue        |
| D            | S3            | B5        |  -1 | issue        |
| A            | S3            | B5        |  -1 | issue        |
+--------------+---------------+-----------+-----+--------------+

此输出是通过以下查询生成的

SELECT
store_officer.officer_name,
tbl_supplier.supplier_name,
store_item.item_name,
store_update_stock_details.qty,
store_update_stock.order_status
FROM
store_update_stock
Inner Join store_officer ON store_officer.officer_id = store_update_stock.supplier
Inner Join tbl_supplier ON tbl_supplier.supplier_id = store_update_stock.supplier
Inner Join store_update_stock_details ON store_update_stock.update_stock_id = store_update_stock_details.update_stock_id
Inner Join store_item ON store_item.item_id = store_update_stock_details.item
WHERE
store_item.item_id =  '3'

然后,我需要通过组合列“ legal_name”和“ supplier_name”来获得以下输出:

+------------------------------+-----------+-----+--------------+
| supplier_name / officer_name | item_name | qty | order_status |
+------------------------------+-----------+-----+--------------+
| S1                           | B5        |  21 | purchase     |
| S1                           | B5        |  20 | purchase     |
| C                            | B5        |  -2 | issue        |
| D                            | B5        |  -1 | issue        |
| A                            | B5        |  -1 | issue        |
| A                            | B5        |  -1 | issue        |
| B                            | B5        |  -1 | issue        |
| C                            | B5        |  -1 | issue        |
| D                            | B5        |  -1 | issue        |
| A                            | B5        |  -1 | issue        |
+------------------------------+-----------+-----+--------------+

可以在我的查询中进行哪些更改以获得所需的输出?谁能帮我 ?

2 个答案:

答案 0 :(得分:0)

您可以使用条件CASE..WHEN表达式来进行相应的确定。

SELECT
  CASE store_update_stock.order_status
      WHEN 'purchase' THEN tbl_supplier.supplier_name 
      ELSE store_officer.officer_name
  END AS supplier_officer_name, 
  store_item.item_name,
  store_update_stock_details.qty,
  store_update_stock.order_status
FROM
  store_update_stock
Inner Join store_officer 
  ON store_officer.officer_id = store_update_stock.supplier
Inner Join tbl_supplier 
  ON tbl_supplier.supplier_id = store_update_stock.supplier
Inner Join store_update_stock_details 
  ON store_update_stock.update_stock_id = store_update_stock_details.update_stock_id
Inner Join store_item 
  ON store_item.item_id = store_update_stock_details.item
WHERE
  store_item.item_id =  '3'

答案 1 :(得分:0)

SELECT IF(store_update_stock.order_status = 'issue', store_officer.officer_name, tbl_supplier.supplier_name) `supplier_name / officer_name`, ...