Example pseudo-database:
Table "items":
PRIMARY item_id number,
FOREIGN factory_id,
name string,
price number,
Table "shops":
PRIMARY shop_id number,
address string
Table "factories":
PRIMARY factory_id number,
address string,
status string
Table "items_in_shops":
FOREIGN item_id,
FOREIGN shop_id
If I want to know which shop.address
has item.name
created on factory.address
, which SQL query I need to use? (For example, MySQL query).
I guess it possible using several queries (pseudo-code):
facotry_address = "example address";
item_name = "toy";
factory_id = SELECT factory_id FROM factories WHERE address = {factory_address};
item_id = SELECT item_id FROM items WHERE name = {item_name} AND factory_id = {factory_id};
shop_ids = SELECT shop_id FORM items_in_shops WHERE item_id = {item_id};
for (shop_id in shop_ids):
shop_address = SELECT addres FROM shops WHERE shop_id = {shop_id};
But how can I access it with only one query? Which MySQL construction should I use?