所以我有一些桌子。 城市:
CI_ID | City
1 | Aberdeen
2 | Abingdon
国家:
CO_ID | Country
1 | United States of America
2 | Australia
省:
PR_ID | Province
1 | Alabama
2 | South Dakota
City_Province
CI_ID | PR_ID
1 | 2
2 | NULL
City_Country
CI_ID | CO_ID
1 | 1
2 | 2
通过创建视图我想拥有
CI_ID | City | Province | Country
1 | Aberdeen | South Dakota | United States of America
2 | Abingdon | NULL | Australia
我无法弄清楚查询。
答案 0 :(得分:1)
JOIN
五个表格,但是LEFT JOIN
Province
表格如下:
创建视图:
CREATE VIEW AllData
AS
SELECT
c.CI_ID,
c.city,
p.Province,
co.Country
FROM City c
INNER JOIN City_Province cp ON c.CI_ID = cp.CI_ID
INNER JOIN City_Country cc ON cc.CI_ID = c.CI_ID
INNER JOIN Country co ON cc.CO_ID = co.CO_ID
LEFT JOIN Province p ON p.PR_ID = cp.PR_ID;
然后,只要您想从该视图中进行选择,就可以像普通表一样从中进行选择:
SELECT * FROM AllData
这会给你:
| CI_ID | CITY | PROVINCE | COUNTRY |
--------------------------------------------------------------
| 1 | Aberdeen | South Dakota | United States of America |
| 2 | Abingdon | (null) | Australia |
了解更多信息: