[英]MySQL combine two columns into one column, one another row
I have following output 我有以下输出
+--------------+---------------+-----------+-----+--------------+
| 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 |
+--------------+---------------+-----------+-----+--------------+
This output has generated using following query 使用以下查询生成了此输出
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'
Then I need to get the following output by combining columns "officer_name" & "supplier_name" as follows : 然后,我需要通过组合列“ 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 |
+------------------------------+-----------+-----+--------------+
What are the changes can be done in my query to get the desired output ?. 可以在我的查询中进行哪些更改以获得所需的输出? Can anyone help me ?
谁能帮我 ?
You can use conditional CASE..WHEN
expressions to determine accordingly. 您可以使用条件
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.