Reputation: 19425
I have the following tables:
| Store | Address | Category | link |
| id | id | id | fk_categoryID |
| fk_addressID | city | category | fk_storeID |
I want to select all stores with address that has category A.
I use this to find stores in a specific city:
SELECT a.name, b.street1, b.street2, b.city
FROM store a
JOIN address b ON b.id = a.fk_addressID
WHERE b.city = 'oslo'
Now I just need to add category criteria.
I can't remember how to do that. Adding another INNER or LEFT join for the link table works, but I get 7-10 rows for each store found.
Can anyone help me please?
Upvotes: 0
Views: 26
Reputation: 29278
Try the following:
SELECT s.id AS "Store ID", a.id AS "Address ID", a.city AS "City", c.category AS "Category" FROM store s
LEFT JOIN address a ON a.id = s.fk_addressID
LEFT JOIN link l on l.fk_storeID = s.id
LEFT JOIN category c on c.id = l.fk_categoryID
WHERE a.city = "oslo" AND c.category = "A";
Let me know if that helps!
EDIT
Recreated the schema on my computer, added some test data, ran the SQL and got the following:
Upvotes: 1