簡體   English   中英

MySQL將兩列合並為一列,另一行

[英]MySQL combine two columns into one column, one another row

我有以下輸出

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

可以在我的查詢中進行哪些更改以獲得所需的輸出? 誰能幫我 ?

您可以使用條件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'
SELECT IF(store_update_stock.order_status = 'issue', store_officer.officer_name, tbl_supplier.supplier_name) `supplier_name / officer_name`, ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM