简体   繁体   English

MySQL:根据多行数据组合多个相关表

[英]MySQL: Combine multiple related tables based on data in multiple rows

[UPDATED] [更新]

I have three tables that are defined by one table and the relationship between those three tables are defined by another table like below: 我有一个表定义的三个表 ,而这三个表之间的关系由另一个表定义,如下所示:

  1. Table #1: wp_pods_cities [Cities PODS] 表#1: wp_pods_cities [城市PODS]

     item_id city -------- ---------- 1 Albany 2 Perth 3 Albany 4 Hollywood 5 Albany 6 San Diego 7 Denpasar 
  2. Table #2: wp_pods_regions [Regions PODS] 表#2: wp_pods_regions [地区PODS]

     item_id region -------- ------------------ 1 Western Australia 2 California 3 Texas 4 Bali 
  3. Table #3: wp_pods_countries [Countries PODS] 表#3: wp_pods_countries [国家PODS]

     item_id country -------- -------------- 1 Australia 2 United States 3 Indonesia 
  4. Table #4: wp_posts [Post Types] 表#4: wp_posts [帖子类型]

     pod_id pod_name post_type ------- ---------- ---------- pod_1 countries pods pod_2 regions pods pod_3 cities pods 
  5. Table #5: wp_podsrel [PODS Relationship] 表#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 

NOTE: The tables above are created in Wordpress by PODS CMS plugin but are simplified to make them readable here. 注意:上表是由PODS CMS插件Wordpress中创建的,但经过简化后可以在此处读取。 The actual tables also have thousands of data. 实际的表也有成千上万的数据。

What I'd Like To Do 我想做什么

I would like to list the same cities which are defined (in this case, Albany ) as well as the regions and thus the countries they belong to. 我想列出已定义的相同城市 (在本例中为Albany ),以及区域以及它们所属的国家

  • expected result: 预期结果:

     city region country ------- ------------------ -------------- Albany Western Australia Australia Albany California United States Albany Texas United States 

What I've Tried 我尝试过的

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

This will get me the cities and their related regions but the countries aren't related. 这将使我了解城市及其相关区域,但与国家无关。 How do I achieve the sample result above? 我如何获得上述样本结果?

Thanks in advanced. 提前致谢。

I managed to get the correct output by using the podsrel table twice, once for city, once for region. 我设法通过两次使用podsrel表来获得正确的输出,一次用于城市,一次用于区域。

SELECT ct.city, rg.region, co.country

FROM wp_pods_cities AS ct, wp_podsrel AS pr

INNER JOIN wp_pods_regions AS rg
    ON rg.item_id = pr.related_item_id

INNER JOIN wp_podsrel AS pr2
    ON pr2.item_id = rg.item_id

INNER JOIN wp_pods_countries AS co
    ON co.item_id = pr2.related_item_id

WHERE ct.city = 'Albany'
    AND pr.item_id = ct.item_id

ORDER BY ct.city

Thanks to @sjdaws 's input I managed to modify his solution for my updated question. 感谢@sjdaws的输入,我设法为我更新的问题修改了他的解决方案

SELECT ct.city, rg.region, co.country
    FROM wp_pods_cities AS ct, wp_podsrel AS pr
        INNER JOIN wp_pods_regions AS rg
            ON rg.item_id = pr.related_item_id
        INNER JOIN wp_podsrel AS pr2
            ON pr2.item_id = rg.item_id
        INNER JOIN wp_pods_countries AS co
            ON co.item_id = pr2.related_item_id
    WHERE ct.city = 'Albany'
        AND pr.item_id = ct.item_id
        AND pr.pod_id = 'pod_3'
        AND pr2.item_id = rg.item_id
        AND pr2.pod_id = 'pod_2'
    ORDER BY co.country, rg.region

Check out the result here: http://www.sqlfiddle.com/#!2/d40c0/10 在此处查看结果: http : //www.sqlfiddle.com/#!2/d40c0/10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM