ido bublil
ido bublil

Reputation: 73

Sql query from related tables

I have 3 tables:

shopping:

id  buyer   fruit
1   1 [->]  2 [->]
2   2 [->]  2 [->]

fruits:

id  fruit
1   apple
2   banana

buyers:

id  buyer
1   ido
2   omri

I want to extract from the table of 'shopping' and put the values of the other tables ​​in the row. For example: Row number one in 'shopping' should look like this:

id  buyer   fruit
1   ido     banana

Upvotes: 7

Views: 7431

Answers (1)

doublesharp
doublesharp

Reputation: 27609

You just need to join the related tables on their respective IDs:

SELECT s.id, b.buyer, f.fruit 
FROM shopping s
JOIN fruits f ON s.fruit = f.id
JOIN buyers b ON s.buyer = b.id

Upvotes: 9

Related Questions