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 :
+------------------------------+-----------+-----+--------------+
| 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.
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`, ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.