我有以下表格:
| Store | Address | Category | link |
| id | id | id | fk_categoryID |
| fk_addressID | city | category | fk_storeID |我想选择所有有A类地址的商店。
我用它在一个特定的城市找到商店:
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'现在我只需要添加类别标准。
我不记得该怎么做了。为链接表添加另一个内部或左联接是有效的,但是我为找到的每个商店获得7-10行。
有人能帮我吗?
发布于 2014-12-19 16:03:31
尝试以下几点:
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";如果这有帮助的话请告诉我!
编辑
在我的计算机上重新创建架构,添加一些测试数据,运行SQL并获得以下内容:

https://stackoverflow.com/questions/27569328
复制相似问题