table 'sales'
id refid invoice itemid price
1 001 1501 ss01 12000
table 'purchase'
id itemid price
1 ss01 10200
table 'reference'
id refid refname
1 001 bedul
我的SQL DB中有这三个表,因此如何使用mysql query将它们变成单个表,如下所示:
table
id refid refname invoice itemid sellprice buyprice profit
1 001 bedul 1501 ss01 12000 10200 1800
最佳答案
您的结果表不够详细,但是您可以尝试以下操作:
select sales.id, sales.refid, reference.refname, sales.invoice,
sales.itemid, sales.price as sellprice, purchase.price as buyprice,
sales.price - purchase.price as profit
from sales
left outer join reference on reference.refid = sales.refid
left outer join purchase on purchase.itemid = sales.itemid
关于mysql - mysql-数据收集多个表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33182460/