首页 > 解决方案 > Joining two tables without a common column but using another table connected to both

问题描述

I have three tables in Mysql

I need to get (balance-qty) for every uid:orderIdcombination for specific type of item. How can I connect table A and table B if they have no common column to join on. But both of them are connected to table C.

标签: mysql

解决方案


With joins between the 3 tables:

select a.uid, a.balance, b.qty
from tablea a 
inner join tablec c on c.uid = a.uid
inner join tableb b on b.orderid = c.orderid
where a.itemtype = ?

You can add more columns if you need them.


推荐阅读