我有以下输出
+--------------+---------------+-----------+-----+--------------+
| 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'然后,我需要通过组合列"officer_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 |
+------------------------------+-----------+-----+--------------+在我的查询中可以做哪些更改来获得所需的输出?有人能帮我吗?
发布于 2018-11-24 16:41:36
您可以使用条件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'发布于 2018-11-24 16:44:41
SELECT IF(store_update_stock.order_status = 'issue', store_officer.officer_name, tbl_supplier.supplier_name) `supplier_name / officer_name`, ...https://stackoverflow.com/questions/53460234
复制相似问题