INNER JOIN 3 tables with pivot table

1

Part of my database consists of these three tables. And I'm trying to get to a SQL query that tells me the clients by sellers, ie for example: John sellers sells for 4 stores (street y, street x, street j, street z) in which each one of these shops belongs to only one customer, in this example the store of the street x belongs to the client Nike, the one of the street and the client Adidas, the one of the street j also belongs to the Nike and the store z belongs to the client Billabong < p>

A store can have more than one seller, eg: John sells tennis and Thomas sells coats at the same store.

Relationships:

sellers- > stores: many to many (pivot table: stores_sellers)

clients-> stores: one for many

Based on the example above I wanted a query that told me John's customers through the stores he owns. Result would be: (Nike, Adidas, Billabong)

    
asked by anonymous 12.02.2016 / 19:54

1 answer

1
SELECT * FROM sellers
INNER JOIN store_sellers ON sellers.id = store_sellers.id_sellers
INNER JOIN store ON store.id = store_sellers.id_store
    
12.02.2016 / 20:04