0

I have three tables in Mysql

  • table A-> uid, itemType, balance,orderType
  • table B-> orderId, itemType, qty
  • table C-> uid,orderId

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.

xQbert
  • 34,733
  • 2
  • 41
  • 62
Sty
  • 39
  • 2
  • 9
  • You can join all three in one SELECT statement. You'd have two JOIN clauses. See [SQL inner join more than two tables](https://stackoverflow.com/questions/14995000/sql-inner-join-more-than-two-tables). – lurker Apr 04 '19 at 18:59

1 Answers1

0

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.

forpas
  • 160,666
  • 10
  • 38
  • 76