UNION我有這個疑問:集團通過與MariaDB的
select item_code, item_name, First, Second, Third, `Fourth` from (
(SELECT t.item_code, i.item_name, t.actual_qty AS `First`, '' AS `Second`, '' AS `Third`, '' AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Finished Goods")
UNION
(SELECT t.item_code, i.item_name, '' AS `First`, t.actual_qty AS `Second`, '' AS `Third`, '' AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Tank 01 - 1200 KG")
UNION
(SELECT t.item_code, i.item_name, '' AS `First`, '' AS `Second`, t.actual_qty AS `Third`, '' AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Tank 02 - 1200 KG")
UNION
(SELECT t.item_code, i.item_name, '' AS `First`, '' AS `Second`, '' AS `Third`, t.actual_qty AS `Fourth` FROM tabBin t JOIN `tabItem` i ON i.name = t.item_code WHERE t.warehouse = "Tank 03 - 1200 KG")) as temp
GROUP BY temp.item_code, temp.item_name, temp.First, temp.Second, temp.Third, temp.Fourth
這是輸出:
item_code item_name first, second, third, fourth
fg-plu PLUM 30.000000
fg-plu PLUM 40.000000
fg-plu PLUM 10.000000
fg-plu PLUM 1248.00
我想組由item_code
和item_name
或只是item_code
。
那麼不要使用UNION,而是一個普通的子查詢......? – CBroe
會返回很多行。我不知道這是否會繼續工作 –
這被稱爲「pivoting」 - 按照我添加的標籤。 –