多対多の関係がある場合 (たとえば、1 つの地区に多くの店舗が含まれている場合や、1 つの店舗が多くの地区に含まれている場合など)、それらのエンティティ間で相互参照テーブルを使用する必要があります。
私は、特定の地区が 1 つの国にのみ含まれている可能性があると想定しています。あなたのシナリオをモデル化する方法は次のとおりです。
countries(country_id [PK], name, ...)
districts(district_id [PK], country_id [FK], name, ...)
districts_has_stores(district_id [PK], store_id [PK])
stores(store_id [PK], name, ...)
categories_has_stores(category_id [PK], store_id [PK])
categories(category_id [PK], name, ...)
ER では:

districts_has_stores
およびcategories_has_stores
は、エンティティ間の多対多の関係を表す相互参照テーブルです。
このモデルに基づいて、特定の国のすべての店舗を取得し、次の SQL を使用して地区名で店舗を並べ替えることができます。
SELECT
c.*
FROM
districts a
INNER JOIN
districts_has_stores b ON a.district_id = b.district_id
INNER JOIN
stores c ON b.store_id = c.store_id
WHERE
a.country_id = <country_id here>
ORDER BY
a.name
各国の店舗数を取得する:
SELECT
a.country_id,
COUNT(*) AS store_count
FROM
districts a
INNER JOIN
districts_has_stores b ON a.district_id = b.district_id
GROUP BY
a.country_id
編集: この回答に対するコメントに従って、category_id を持つすべてのストアを取得する1
方法の例を次に示します。
SELECT
b.*
FROM
categories_has_stores a
INNER JOIN
stores b ON a.store_id = b.store_id
WHERE
a.category_id = 1
特定の category_id ( ) 内のすべての店舗を取得し1
、結果をフィルタリングして、地区4
または5
内の店舗のみを含めます。
SELECT DISTINCT
b.*
FROM
categories_has_stores a
INNER JOIN
stores b ON a.store_id = b.store_id
INNER JOIN
districts_has_stores c ON b.store_id = c.store_id
WHERE
a.store_id = 1 AND
c.district_id IN (4,5)