0

我正在上一门数据库课程,但我有一个似乎无法正确回答的问题。

有3张表:

country(code, iso_abbreviation, name)
area(name, city, country_code, latitude, longitude, elevation)
attraction(name, type, city, country_name, latitude, longitude, elevation)

现在,问题是这样问的:在景点表和区域表中都可以找到区域。列表

(country_abbreviation, area_name, latitude, longitude, elevation)

适用于海拔 5000 英尺以上的所有区域。由于区域和景点数据之间可能存在一些不一致,因此纬度、经度和海拔可能会有所不同。在这种情况下,显示数据的两种变体。

所以我想出了下面的查询,但我不确定它是否正确地将它们配对,它也不会将数据分成两行,其中一个 ( latitude, longitude, elevation) 元素是不同的。

SELECT country.iso_abbreviation as country_abbreviation, area.name as name, 
area.latitude, area.longitude, area.elevation
FROM area JOIN country on country.code = area.country_code
      JOIN attraction on area.name = attraction.name
WHERE area.elevation > 10000

UNION 

SELECT DISTINCT country.iso_abbreviation as country_abbreviation, area.name, 
attraction.latitude, attraction.longitude, attraction.elevation
FROM area JOIN country on country.code = area.state_code
      JOIN attraction on area.name = attraction.name
WHERE attraction.elevation > 10000 ORDER BY country_abbreviation
; 

有人可以帮我解决这个问题吗?

4

1 回答 1

0

这会做你所描述的:

WITH cte AS (
   SELECT c.iso_abbreviation AS country_abbreviation
        , a.name, a.latitude, a.longitude, a.elevation
   FROM   area       a
   JOIN   country    c ON c.code = a.country_code
   WHERE  a.elevation > 5000
   )
SELECT * FROM cte
UNION
SELECT c.country_abbreviation
     , t.name, t.latitude, t.longitude, t.elevation
FROM   cte        c
JOIN   attraction t USING (name)     -- assuming name links area & attraction (?)
ORDER  BY country_abbreviation, name -- (?)

但老实说,桌子的布局以及你被赋予的任务似乎都不清楚。

于 2013-10-21T19:34:39.920 回答