mysql - 数据收集多个表? [英] mysql - data collection multiple table?

查看:30
本文介绍了mysql - 数据收集多个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 数据库中有这三个表,那么如何使用 mysql 查询将它们变成如下所示的单个表:

I have this three table in my sql DB, so how to make them turn into single table like below using mysql query :

table 

id refid refname invoice itemid sellprice buyprice profit
1  001   bedul   1501    ss01   12000     10200    1800

推荐答案

你的结果表不够详细,但你可以试试这个:

Your result table is not detail enough but you can try this :

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 - 数据收集多个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆