简体   繁体   English

如何从多个 MySQL 表中提取数据

[英]How can I pull data from multiple MySQL tables

I am making these three MySQL queries consecutively to pull the information I need, then I combine them in excel.我连续进行这三个 MySQL 查询以提取我需要的信息,然后将它们组合在 excel 中。 Although, I believe there is a better way to make one single query and get all the information at once.尽管如此,我相信有一种更好的方法可以进行一次查询并立即获取所有信息。 I am totally lost with JOIN statements, so could anyone please help me to construct the query I am looking for?我完全迷失了 JOIN 语句,所以有人可以帮我构建我正在寻找的查询吗?

All I am trying to pull is the field "inventory_cost.cost".我想要提取的只是“inventory_cost.cost”字段。

SELECT 
    sku,
    UCASE(metal),
    SUM(ABS(qty+s5qty+s6qty+s7qty+s8qty+s9qty+s10qty+s11qty+s12qty+s13qty)) AS qtySold 
    FROM inventory_history
    WHERE reason = "order"  
    GROUP BY sku,metal 
    ORDER BY sku,metal ASC


SELECT sku,UCASE(metal),id FROM inventory GROUP BY sku,metal ORDER BY sku,metal ASC

SELECT inv_id,cost FROM inventory_cost ORDER BY inv_id

All your help is most appreciated!!非常感谢您的所有帮助!!

Burcin布尔辛

SELECT sku, UCASE(metal), 
SUM(ABS(qty+s5qty+s6qty+s7qty+s8qty+s9qty+s10qty+s11qty+s12qty+s13qty)) AS 
qtySold 
,cost = ic.cost * 
SUM(ABS(qty+s5qty+s6qty+s7qty+s8qty+s9qty+s10qty+s11qty+s12qty+s13qty))
FROM inventory_history ih join inventory inv on inv.sku=ih.sku
join inventory_cost ic on ic.inv_id=inv.id
WHERE reason = "order"
GROUP BY sku,metal
ORDER BY sku,metal ASC

Let me know if you have any more queries.如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM