简体   繁体   English

Oracle从多个表中选择?

[英]Oracle Select from multiple Tables?

I have multiple Tables in Oracle which have primary keys and foreign keys linking to each other. 我在Oracle中有多个表,它们具有相互链接的主键和外键。 The tables are: 表格是:

Continent
Sub-Continent
Country
Region
City
Location

As example of how the layout is for each: 作为每个布局的示例:

continent_id      | continent_name
1                 | Africa
2                 | America

sub_continent_id  | sub_continent_name  | continent_id
A1                | Southern Africa     | 1
B1                | Western Africa      | 1
A2                | North America       | 2
B2                | South America       | 2

country_id        | country_name        | sub_continent_id
CAN               | Canada              | A2
ZA                | South Africa        | A1

This continues for the rest: Region , City and Location . 其余部分继续: RegionCityLocation As can be seen, the ID's reference another table. 可以看出,ID的引用是另一个表。 So: 所以:

country_name "Canada" references sub_continent_id "A2" which is "North America". country_name “Canada”引用sub_continent_id “A2”,即“北美”。 "North America" references continent_id 2 which is "Americas". “北美”引用了continent_id 2,即“Americas”。

What I want to try and do is to run a SQL query that if I chose a location, it should return the rest of the values from other tables. 我想要尝试做的是运行一个SQL查询,如果我选择了一个位置,它应该返回其他表中的其余值。 So if I select a location called "Triange Building", it will automatically return: 因此,如果我选择一个名为“Triange Building”的位置,它将自动返回:

Triangle Building, New York City, New York, North America, Americas

I am uncertain of what query to run to collect all these values each time. 我不确定每次运行什么查询来收集所有这些值。

I can run a single query like this to show me region name and city name, but do not know how to select more values from more Tables. 我可以像这样运行一个查询来显示区域名称和城市名称,但不知道如何从更多表中选择更多值。

SELECT E.city_name NAME,D.region_name DNAME
FROM CITY E JOIN REGION D
ON (E.region_id = D.region_id);

Just join all the tables 只需加入所有表格

SELECT E.city_name as NAME, D.region_name as DNAME, C.COUNTRY_NAME, SC.SUB_CONTINENT_NAME, 
       CO.CONTINENT_NAME
FROM  CITY E JOIN REGION        D  ON  E.region_id        =  D.region_id
             JOIN COUNTRY       C  ON  D.country_id       =  C.country_id
             JOIN SUB-CONTINENT SC ON  C.sub_continent_id = SC.sub_continent_id
             JOIN CONTINENT     CO ON SC.continent_id     = CO.continent_id;

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

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