[更新]
我有三个由一个表定义的表,这三个表之间的关系由另一个表定义,如下所示:
表 #1:wp_pods_cities [城市 PODS]
item_id city -------- ---------- 1 Albany 2 Perth 3 Albany 4 Hollywood 5 Albany 6 San Diego 7 Denpasar
表 #2:wp_pods_regions [区域 PODS]
item_id region -------- ------------------ 1 Western Australia 2 California 3 Texas 4 Bali
表 #3:wp_pods_countries [国家 PODS]
item_id country -------- -------------- 1 Australia 2 United States 3 Indonesia
表#4:wp_posts [帖子类型]
pod_id pod_name post_type ------- ---------- ---------- pod_1 countries pods pod_2 regions pods pod_3 cities pods
表#5:wp_podsrel [PODS 关系]
rel_id pod_id item_id related_pod_id related_item_id ------- ------- -------- --------------- ---------------- 1 pod_2 1 pod_1 1 2 pod_2 2 pod_1 2 3 pod_2 3 pod_1 2 4 pod_2 4 pod_1 3 5 pod_3 1 pod_2 1 6 pod_3 2 pod_2 1 7 pod_3 3 pod_2 2 8 pod_3 4 pod_2 2 9 pod_3 5 pod_2 3 10 pod_3 6 pod_2 3 11 pod_3 7 pod_2 4
注意:上面的表格是由PODS CMS 插件在Wordpress中创建的,但经过简化以使其在此处可读。实际的表也有数千条数据。
我想做的事
我想列出已定义的相同城市(在本例中为Albany)以及它们所属的地区和国家。
预期结果:
city region country ------- ------------------ -------------- Albany Western Australia Australia Albany California United States Albany Texas United States
我试过的
SELECT ct.city, rg.region, ctr.country
FROM wp_pods_cities AS ct
INNER JOIN wp_podsrel AS rel ON ct.item_id = rel.item_id
AND rel.pod_id IN ('pod_3', 'pod_2')
INNER JOIN wp_pods_regions AS rg ON rel.related_item_id = rg.item_id
INNER JOIN wp_pods_countries AS ctr ON rel.related_item_id = ctr.item_id
WHERE ct.city = 'Albany'
ORDER BY ct.city
这将为我提供城市及其相关地区,但国家不相关。如何实现上述示例结果?
先谢谢了。