INNER JOIN 3 tables with pivot table

Asked

Viewed 341 times

1

inserir a descrição da imagem aqui

Part of my database consists of these three tables. And I’m trying to come up with an SQL query that tells me the clients by sellers (sellers), i.e., for example: João (sellers) sells to 4 stores (street y, street x, street j, street z) in which each of these stores belongs only to one customer, in this example the shop of street x belongs to the customer Nike, that of street y to the customer Adidas, that of street j belongs also to the Nike and the shop z belongs to the customer Billabong

A store can have more than one seller, e.g.: João sells tennis and Tomás sells coats to the same store.

Relations:

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

clients->Stores: one for many

Based on the example above, I wanted a query that would tell me João’s customers through the stores he owns. The result would be: (Nike, Adidas, Billabong)

  • 1

    Post what you’ve tried to do

  • I tried with eloquent (Laravel) but I have to go with raw sql since there is no this default relation in Laravel

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.