0

我正在使用联合,当这样写查询时,

    SELECT tour_date AS "Departure Date",  site_name "Site Name" FROM partres, reservation, tour, site
WHERE partres.res_id = reservation.res_id
  AND reservation.tour_id = tour.tour_id
  AND tour.site_id = site.site_id
GROUP BY tour_date, site_name
HAVING COUNT(part_id) > 7

  UNION

SELECT tour_date AS "Departure Date",  site_name "Site Name"
FROM (
  SELECT res_id, (res_partcost +NVL(RES_GEARCOST,0)) AS "total_cost" 
  FROM reservation) tour, site
WHERE  reservation.tour_id = tour.tour_id
  AND tour.site_id = site.site_id
  AND total_cost > 230
GROUP BY tour_date, site_name;

但它显示错误为

ORA-00904: "TOUR_DATE": invalid identifier

00904. 00000 - “%s:无效标识符” *原因:
*操作:行错误:305 列:10

所以我不知道为什么我现有的专栏有错误。

4

1 回答 1

0

在联合tour_date后半部分没有来自该子选择的列。

在这种情况下,您的tour“表”不是实际的表,而是仅提供res_idand的内部选择的结果total_cost

我的意思是:

SELECT tour_date AS "Departure Date",  site_name "Site Name"
FROM (
  SELECT res_id, (res_partcost +NVL(RES_GEARCOST,0)) AS "total_cost" 
  FROM reservation) tour, site ...

会给你两个“表”来获取列。第二个表site可能site_name是从哪里来的,尽管你真的应该明确地强制执行它,site.site_name因为它使它更明显。

现在这很重要。即使您一个名为 的真实表tour,但您选择的事实是:

(SELECT res_id, (res_partcost +NVL(RES_GEARCOST,0)) AS "total_cost"
 FROM reservation) tour

意味着您的查询所看到tour的不是tour表格。它实际上是括号内的查询。因此tour,在这种情况下,有列res_idtotal_costtour_date看不到。

如果要从子选择中选择内容,则需要确保名称匹配。

于 2013-05-29T05:54:50.613 回答